Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Run javascript method only once?

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67651
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67651
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 344
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()"
reply
    Bookmark Topic Watch Topic
  • New Topic