• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Run javascript method only once?

 
Johannes Thorén
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I got this code in my .jsp page. The intent is to add ?language=en to the url, for example. But the method seems to be called all the time so the url just builds
?language=en?language=en?language=en?language=en.. and so on.

How could i solve the problem?

kind regards,
Drace
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will get called each time the page loads. Perhaps you need to check if the parameter is already on the URL before putting on there again?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


That code is wrong.

First it is onload and not onLoad.
It is saying execute the function extendSearch and store whatever it returns to onload.

If your code was window.onLoad = extendSearch;, it would not of run.

Eric

 
Johannes Thorén
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Pascarello wrote:

That code is wrong.

First it is onload and not onLoad.
It is saying execute the function extendSearch and store whatever it returns to onload.

If your code was window.onLoad = extendSearch;, it would not of run.

Eric



But the method still runs, and if i change it to onLoad or onload, gives the same result
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johannes Thorén wrote:But the method still runs, and if i change it to onLoad or onload, gives the same result

The method runs inline, not as the unload handler.

And just because something works, doesn't make it right. Use unload. The browsers are really lenient about errors, but that doesn't mean that errors should be purposefully made!
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IE might be more forgiving about the Javascript (which it shouldn't because many HTML/Javascript errors go undetected that way), but it doesn't work in Firefox. Use the correct Javascript objects and method. The following worked for me:
1) change window.onLoad to window.onload
2) remove line extendSearch = Function("");
3) add check to see if argument has not yet been included in URL: if (window.location.href.indexOf("language=") < 0) {}
4) Use "window.location.href=" instead of "window.navigate()"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic