aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Run javascript method only once? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Run javascript method only once?" Watch "Run javascript method only once?" New topic
Author

Run javascript method only once?

Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64


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

Joined: Jan 10, 2002
Posts: 61761
    
  67

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6


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

Joined: Nov 18, 2008
Posts: 64
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

Joined: Jan 10, 2002
Posts: 61761
    
  67

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

Joined: Feb 07, 2012
Posts: 344

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()"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Run javascript method only once?