window.location on a Mac
Sometimes in JavaScript, you may want to redirect a user to another URL. This is often done setting the window.location properly like so:
window.location = "https://snook.ca/';
However, Safari and IE 5.2 for Mac (at least on OSX) behave a little differently than browsers on the PC. I put together this basic test page in order to see which scenarios work and which don't.
<script type="text/javascript">
function test3(){
window.location = 'https://snook.ca/';
}
</script>
<p><a href="#"
onclick="window.location = 'https://snook.ca'">
test 1</a></p>
<p><a href="#"
onclick="window.location = 'https://snook.ca';
return false;">test 2</a></p>
<p><a href="#"
onclick="test3();">test 3</a></p>
<p><a href="#"
onclick="test3();return false;">test 4</a></p>
<p><a href="javascript:window.location =
'https://snook.ca';">test 5</a></p>
Tests 2, 4 and 5 work but 1 and 3 don't. And you can probably see why. In 1 and 3, we set the window.location
to a new URL. This would normally redirect the user to the new page. But after the onclick event, it runs the URL contained in the href and overrides the redirect. In this case, the # keeps the page from redirecting altogether. By putting return false;
we stop the href from being processed altogether.
Conversation
Thanks! I was stumped for a while as to why my JS was failing on stupid Macs. This helped.
Thank you! I was pounding my head against the wall on this one.
Thank you so much!!! This seriously was giving major headaches!!!
Thanks a million. I would have had a mjor problem on my hands without this information.
Thanks! this helped out a ton, I was stumped...and blaming it on MACs! ;)
Hallelujah!!!!!!
Thank you sooooo much for posting this. It really helped me a lot!!!!
now that's what the web is for. i was almost weeping for a solution. standardisation means i have to write better code!
This information is very very helpful. It works perfectly with Mac IE5.2. But it does NOT work with Safari on Mac. I would appreciate it if you can give me some suggestion about Safari.
zadar: look into
window.open
or usetarget="_blank"
on links.kxZdtE fkjgh62vDfulv0s5FvSa
fSlZQM blkOpwF7nnBsJ42K7La