0

Im having problems with this simple code:

Javascript:

<script>
window.onload = function(){
  document.getElementById("grenaa").click();
}
</script>

HTML:

<a href="#" id="grenaa" data-filter=".term-grenaa">Menukort - Grenaa</a>

I want to click the link when the page is done laoding. It dosen't seem to work in Safari. Here you can get the idea: www.friskfisken.dk/take-away . The page won't accept jQuery only plain Javascript.

user2389974
  • 33
  • 1
  • 7

3 Answers3

0

It seems that if there hasn't user's behavior, some action is prevented. see how-can-i-trigger-a-javascript-event-click

One exception: The click() method will not cause an element to initiate navigation as if a real mouse-click had been received. see : MDN's HTMLElement.click()

if the onload event is not fired,you should set the cache-control.

<head>
<meta charset="utf-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">

or add a tag in the body:

<iframe style=”height:0px;width:0px;visibility:hidden” src=”about:blank”>
this frame prevents back forward cache
</iframe>
Community
  • 1
  • 1
任富飞
  • 1
  • 2
-1

The way to answer/invoke a click event is written like this (see this jsfiddle):

document.getElementById("grenaa").onclick = function() {
    alert("Hello there!");
};

Or, if you want to invoke a click, use onclick();

document.getElementById("grenaa").onclick();
Allov
  • 1,308
  • 1
  • 13
  • 23
  • I want to click the link "grenaa" when the page loads: The problem is that it isnt working i Safari. See link: friskfisken.dk/take-away – user2389974 May 16 '13 at 13:05
  • The answer mention to use onclick()... see the jsfiddle: http://jsfiddle.net/yDGuJ/3/ – Allov May 16 '13 at 13:10
  • This dosent answer my question? :-) – user2389974 May 16 '13 at 13:11
  • Here, this may help you: http://stackoverflow.com/questions/6682302/onclick-not-working-in-safari – Allov May 16 '13 at 13:12
  • Also this: http://stackoverflow.com/questions/6157929/how-to-simulate-mouse-click-using-javascript/6158050#6158050 – Allov May 16 '13 at 13:13
-1

If you want a certain page as the front page on your website your doing it wrong.

You don't need to use Javascript for that.

This can be done in the .htaccess file in the root of your website (if your using Apache).

DirectoryIndex restaurant-grenaa
scootergrisen
  • 1,003
  • 1
  • 11
  • 20
  • Thank you for the reply.. Doesn't work.. I want a specific take away menu to be clicked when the page is done loading. Try following this link with Chrome www.frsikfisken.dk/take-away and look at the menu when the page loads. This works perfectly in Chrome but i Safari it fails – user2389974 May 16 '13 at 13:36
  • Sorry friskfisken.dk/take-away – user2389974 May 16 '13 at 13:36
  • When the page first loads there is showned a menu before any links get clicked. Where in the code is this menu chosen ? – scootergrisen May 16 '13 at 14:04
  • I see what youre thinking. I tried to to find the specific code that "chooses all items" before the Grenaa link gets clicked, but it seems to be hard to change.. I'll try to find it again and let you have a look – user2389974 May 17 '13 at 06:55
  • Here you can download the file: http://www.friskfisken.dk/page-portfolio-col4.zip I think the place that need changing is somewhere around "filters" line 30. – user2389974 May 17 '13 at 07:37
  • When clicking on the 2 menukort links a class called active is added to the link. I don't see the word "active" anywhere in that code. But anyway if it's made in wordpress it would proberly be better to use the webinterface to edit the website instead of changing the code directly. Have you tryed looking in wordpress and se if it's possible to make the change there ? – scootergrisen May 17 '13 at 15:28