Search Support

Avoid support scams. We will never ask you to call or text a phone number or share personal information. Please report suspicious activity using the “Report Abuse” option.

Learn More

I'm trying to imlement <body onload=' location.href="#tab1" '> on my website here: http://www.vandf.gr - to open #tab1 as the page loads. Any suggestions?.

  • 7 replies
  • 1 has this problem
  • 284 views
  • Last reply by DoctorV

more options

This body tag used to work in Firefox and currently does in Chrome. (Should work everywhere.) It's probably a simple solution, but I haven't found it yet. A refresh in Firefox fixes it in fact... I've also tried replacing "#tab1" with the complete URL, but no dice. Thanks for any ideas...

This body tag used to work in Firefox and currently does in Chrome. (Should work everywhere.) It's probably a simple solution, but I haven't found it yet. A refresh in Firefox fixes it in fact... I've also tried replacing "#tab1" with the complete URL, but no dice. Thanks for any ideas...

Chosen solution

Well, I think this:

<body onload="javascript to run">

is a shorthand way of doing this:

<script> document.body.addEventListener("load", function(){javascript to run}); </script>

So it is a script already. But you could try the more formal approach and see whether that helps. https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener

Read this answer in context 👍 0

All Replies (7)

more options

Is the problem that the script doesn't run (starting at some point after refresh), or it doesn't scroll to the correct location in the page or trigger the desired action? The address bar should indicate whether the script ran, as the hash should show there.

Does it make any difference if you clear Firefox's cache?

See: How to clear the Firefox cache

If you have a large hard drive, this might take a few minutes. If you do not see the number going down on the page, you can reload it using Ctrl+r to check progress.

more options

Thanks for your reply. :) As it is now, it's not a script - just an html body tag. ... I want #tab1 to load when the page loads, Maybe I should try some javascript to do this?

more options

Chosen Solution

Well, I think this:

<body onload="javascript to run">

is a shorthand way of doing this:

<script> document.body.addEventListener("load", function(){javascript to run}); </script>

So it is a script already. But you could try the more formal approach and see whether that helps. https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener

more options

Thanks again for that :) I'm now seeing that my code got a bit messed up and my problem is likely not where I thought it was. I'll work on it some more and post back...

more options

-been working on this, but no luck yet :( I tried seversl Javascript variations of my previous code, but same results as before. The page needs a refresh to select '#tab1' and display correctly. It works everywhere except in Firefox, but if I launch the same page from the locally stored file (on the computer) it always works just fine. This is just getting more mysterious and unsolvable it seems.

<script> function myFunction() {

  window.location.href='#tab1'; 

}

 </script>
 </head>

<body onload="myFunction()">

more options

The address bar correctly displays: http://www.vandf.gr/#tab1 but the browser refuses to display it until a refresh!

more options

It's solved! My browser homepage was mislabeled (previous version). This was a dumb error on my part, there was never anything wrong :)