I need to execute a javascript before the page is start to load? how can i do it? Atfirst my script should execute, after completion of script, if i need i should load the page else i shouln't load it. For example if my script redirect to another page i shouldnot load the current page else i should load it.
simply write your script in the script tag as a first element in body tag:
-- updated --
for hiding other page elements, use div with full width and height, if you want to show the page, hide the div, that will fix the problem
Script tags are execute as soon as they are encountered. Just put your script tag early, and then use a redirect if your condition is true. Hooking the window.onload event handler is how you get Javascript to trigger on load.
Do it server side ...
The logic you present, seems to fit better at the server side, if you really want to avoid the loading of the page completely..
Is it possible for you to serve a page initially which contains merely the JavaScript which you need to execute within an HTML document? You can then request the page you want based on the result of the JavaScript.
You can't run a script before the page loads (without messing up the document type), but you can put the script early in the page, i.e. in the head
section.
You can't stop the page from loading until your script has finished. The page will continue to load in the background, but it will not render until the script has finished.
If you do a redirect in your script, the page will stop loading, but what's already loaded will render while waiting for the new response to arrive.