SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict Phil-man's Avatar
    Join Date
    Nov 2000
    Posts
    291
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    String Replacement or Evading the Base Tag

    I have a page that uses a "base" tag (that I have to leave in place), and I need to figure out how to get around it for purposes of using bookmark links. In other words, if I have a link that goes like this...

    Code:
    ...href="#bookmark"...
    ...it goes to the page defined in the base tag instead of to the bookmark on the current page. This is not what I want. I can't hard-code the current page URL, because I need to execute some Javascript on the page when the link is clicked, and using a hard-coded URL causes the page to reload, and then the executed Javascript is lost.

    I was able to get it to work doing this...

    Code:
    ...self.location=self.location + '#bookmark'...
    ...but that's no good because subsequent clicks on the link result in "#bookmark" being appended to the URL over and over again and then it doesn't work. That is...

    blahblahblah.html#bookmark#bookmark#bookmark, etc.

    So I thought I would do string replacement to get rid of existing instances of the bookmark contained in self.location, and then add it back once...

    Code:
    <script type="text/javascript">
    function goToBookmark()
    {
      var newSelf=self.location;
      newSelf=newSelf.replace(/#bookmark/g,'');
      newSelf=newSelf + '#bookmark';
      self.location=newSelf;
    }
    </script>
    ...but I get an error saying that newSelf is undefined and it bounces me to the other page that I don't want.

    I may be doing numerous things wrong here, but I just want to use a simple bookmark that doesn't get screwed by the base tag! Can anyone help?

    Thanks!

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Why not use self.location.href instead of self.location

    Using self.location.hash to reset the anchor might also work.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Addict Phil-man's Avatar
    Join Date
    Nov 2000
    Posts
    291
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not use self.location.href instead of self.location
    Because I had never heard of it. Nevertheless, I tried it, and the behavior was exactly the same.

    Using self.location.hash to reset the anchor might also work.
    HOLY CRAP! That's perfect!

    self.location.hash="bookmark";
    What could be easier? Thanks!

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    What could be easier?

    self refers to the current window, and window is the default reference, so you should be able to use just the following:

    Code javascript:
    location.hash="bookmark";
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •