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

try

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

that is what I did try. It flickered also.

try

window.location.href = location;

Here is what I have now. It seems to work.


<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

Of course I forgot the return false. BTW you only need one of:

<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

<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>

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

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

loadUrl

yes, that fixed it.

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

//Ukiah

Sorry no time…

Brainwave!!! :rolleyes:

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.


<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?

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