Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing base href

Status
Not open for further replies.

talrnu

Programmer
Dec 19, 2009
4
US
Hi folks, I'm creating a personal site and am attempting to do so without the use of any server-side scripting for various reasons. I have two directories - one for development, which is set up not to be readily available or visible to the general public, and another for the actual site. I do all my poking, prodding, and testing in the development directory, and upload the finished product on the "live" directory (I suspect this isn't uncommon methodology, but I felt the need to explain anyway). Up until now, I've used ASP for many of my scripting needs and seen no problems.

The problem I have now involves changing the href value of the base tag on my site's pages. I want to automate this process with JavaScript so that I can swiftly transfer changed files from my development directory to my live directory without having to swap the base href value back and forth to match the files' locations.

I know how to change the value of the href property of the base object... however, I believe the script is doing this *after* all the assets on the page have been loaded. This means the browser is attempting to load everything from the default base URL before the script has the chance to change it. As a result, the page renders without any assets (images are broken, all styling is gone, etc.). I'm not really sure what to do about this... can anyone give me a tip? Is there some way to reload a page's assets efficiently without reloading the whole page?
 
 

You could do something like the following. This was on all of my Geocities pages for years and worked flawlessly between development and publication versions.

However, your pages probably won't validate and this may not be the best practice. Put your adjusted variety of this in between your <HEAD> and </HEAD> tags and see if that solves your problem.

[tt][blue]
<SCRIPT LANGUAGE='javascript'><!--
var a = "host";
eval(a + " = location." + a);
a = (host == "") ? "/MHD/Desktop Folder/GeoCity" : "";
a = (host == " ? "/Athens/Thebes/6620" : a;
var bref = location.href.substring(0, 7) + host + a + "/";

document.writeln();
document.writeln("<BASE HREF='" + bref + "'>");
// --></SCRIPT>
[/blue][/tt]


mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
 
Be aware that if the viewer has javascript turned off, he will get a page of garbage served up by his browser. I don't have any idea how you can resolve that problem if you use the above dynamic BASE HREF javascript code since you are relying on their javascript to be turned on to generate the BASE HREF link.

If their javascript is turned off, their browser will ignore the above javascript code and the pages will not contain a BASE HREF of any kind.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
Hi

Personally I would use some kind of template engine to generate those static pages, either one of the readily available template engines or a handmade one. Then when generating the HTML files, a parameter or setting would tell the engine what value to put for the [tt]base[/tt] [tt]href[/tt].

Or I would use a command-line tool to replace the [tt]base[/tt] [tt]href[/tt]s in all files in a given directory.

Anyway, personally I would not imply JavaScript in this.


Feherke.
 
Hey mmerlin, thanks for your reply. I'm getting around the "javascript disabled" problem by first using plain ol' html to declare the base href value to coincide with the live version of my site. Then my script checks to see if it's being executed on the live site or on my development directory. In the latter case, it updates the href value of the base tag. So even if that script never runs, Average Joe will always see the right page, and as long as I don't decide to disable javascript, I should theoretically have no problem either. If your script works but mine doesn't, does that mean using document.write somehow makes the script execute before the rest of the page's assets are loaded? Or maybe creating a new base tag after the fact forces already-loaded assets to re-evaluate their urls? Otherwise, I don't see how it works better...

Hi feherke, thanks to you as well. Your solution certainly makes sense, but I fear I'm a bit masochistic and really enjoy building everything from scratch in a (comparatively) low-caliber text editor. My hobby isn't so much efficient web development as it is writing the code... Sure, it'd be easy to use some find-replace macro, but my goal here is to *entirely* automate the process. Sorry, I guess I should have specified that I'm more interested in a programming solution...
 
Solved!

Instead of messing with the current base tag's href value, I tried using document.write to create an entirely new base tag when the script is run on my development directory. It works! Not sure how technically having two base tags might affect the site's legitimacy though... Thanks again guys.
 
There's a good chance this site won't stay hosted on its current server for more than a year or two. I don't have admin privileges on this server, so I can't install PHP (which I prefer for server-side scripting). When I do move the site, it'll theoretically be to a server I have admin powers over, and then I can use PHP. Instead of using ASP now and porting to PHP later, I'm just avoiding ASP altogether (sticking it to the man and all, you know :D). Anyway, I have little experience with JS, so this is a great learning opportunity.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top