SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    load url with javascript

    I want to use javascript to load a new url, in the same window as the calling link, when a user clicks on a link. The code that I have loads the location then flickers and loads the original page that the link was on. I am testing this in mozilla 1.2.1.



    <script type="text/javascript">
    function loadUrl(location)
    {
    this.document.location.href = location;
    }
    </script>



    snip---html----

    <a href="#" onclick="loadUrl(http://somesite.com)">link</a>

    <a href="#" onclick="loadUrl('http://somesite.com')">link</a>

    --end--snip

  2. #2
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try
    Code:
    <a href="#" onclick="loadUrl('http://somesite.com')">link</a>
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  3. #3
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that is what I did try. It flickered also.

  4. #4
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try
    Code:
    window.location.href = location;
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  5. #5
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is what I have now. It seems to work.

    Code:
    <script type="text/javascript">
    function loadUrl(newLocation)
    {
    window.location = newLocation;
    return false;
    }
    </script>
    
    
    
    snip---html----
    
    <a href="javascript:void" onclick="loadUrl('http://somesite.com'); return false;">link-1</a>
    
    <a href="javascript:void" onclick="loadUrl('http://somesite-2.com'); return false;">link-2</a>
    
    --end--snip

    thanks //Ukiah Smith

  6. #6
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course I forgot the return false. BTW you only need one of:
    Code:
    <script type="text/javascript">
    function loadUrl(newLocation)
    {
      window.location = newLocation;
    }
    </script>
    
    <a href="javascript:void" onclick="loadUrl('http://somesite.com'); return false;">link-1</a>
    or
    Code:
    <script type="text/javascript">
    function loadUrl(newLocation)
    {
      window.location = newLocation;
      return false;
    }
    </script>
    
    <a href="javascript:void" onclick="return loadUrl('http://somesite.com');">link-1</a>
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  7. #7
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    The script acts funny when I don't have both 'return false;' statements.

    Check out my actually code.
    http://www.societyofno.com/index.html
    http://www.societyofno.com/script.js

    Some of the links on the page don't work. The only links that I am doing this onclick javascript stuff on are the 'Read More' links.

    The first 'Read More' link loads the correct page, but disables the back button after it loads the correct page. This happened when the main page was reloaded and then I clicked on the first 'Read More' link.

    The 'Read More' link for the "trend pimp" article doesn't work. To my eyes it looks exactly like the other links. What did I do wrong.

    //Ukiah

  8. #8
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by societyofno
    The script acts funny when I don't have both 'return false;' statements.
    Must be some other problem, cos they certainly aren't needed.

    Your other problem is because you haven't put in the call to
    Code:
    loadUrl
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  9. #9
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, that fixed it.

    Did you see anything else that might have been screwing with the script?

    //Ukiah

  10. #10
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry no time...
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  11. #11
    SitePoint Wizard Bill Posters's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Brainwave!!!

    Given that you appear to be reproducing a standard link, why not use one?

    What is the reason that you're using javascript to do this?

    Given that the destination url is still hardcoded into the link, there seems to be no reason why you are using links that are inaccessible and dead to search engines when standard links will do exactly the same thing without the cons.

    At the very least you should replace javascript: void (which is a nasty waste of space) with another copy of the destination url.

    Code:
    <script type="text/javascript">
    function loadUrl(newLocation)
    {
      window.location.href = newLocation;
    }
    </script>
    
    <a href="http://somesite.com" onclick="loadUrl(this.href); return false;">link-1</a>
    It's also worth keeping in the window.location.href as this means the page change is logged to the browser's history so can be navigated using the back and forward buttons.

    Still, I can't see why you're using javascript at all.

    What am I missing?
    New Plastic Arts: Visual Communication | DesignateOnline

    Mate went to NY and all he got me was this lousy signature

  12. #12
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess I should have explained myself a little better. I am trying to keep my sites PageRank by not having real links. Here is more info on PageRank Holes.

    So basicly I want a normal looking link that a user can use to get to a site. Yet not have google follow the link when spidering my pages.


    cheers //Ukiah


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
  •