Khan
Khan

Reputation: 21

Onclick auto popup code

I want to know that how can i create a JavaScript code that must open a new page at click anywhere at website. currently i am using onload script.

<script type="text/javascript">
function open_on_entrance(url,name)
{
   window.open('http://realestatepakistan.pk','Real Estate Pakistan')
}
</script>
<body onload="open_on_entrance()"></body

but onload is not effective as it opens very low number of pages. A must must open page script needed fro my web http://pkr.com.pk/.

Upvotes: 2

Views: 962

Answers (2)

Larry Lane
Larry Lane

Reputation: 2191

Place this code in a script tag right before your closing </body> tag that way it will load after the dom has loaded. You will notice that I removed your parameters from your function because you were not using them inside of your script. Make sure you change the values of window.open to the values you need for your script.

JavaScript:(live example)

function open_on_entrance() {    

  window.open('http://bytewarestudios.com','Welcome to Byteware Studios');

}

document.addEventListener("click", function() {
  open_on_entrance();
});

If you decide to the add the parameters back in make sure you supply them to your function when you call it.

Example Using Parameters:

function open_on_entrance(url,name) {    

     window.open(url,name);

}

document.addEventListener("click", function() {

      var url = 'http://bytewarestudios.com';

      var name = 'Welcome to Byteware Studios';

      open_on_entrance(url,name);
});

Upvotes: 0

Ammar Hasan
Ammar Hasan

Reputation: 2516

This code will open the popup when you click anywhere on website.

<script type="text/javascript">
var shouldOpenWindow = true;
function open_on_click(url,name) {
   if (shouldOpenWindow) {
      window.open('http://realestatepakistan.pk','Real Estate Pakistan');
      // if you want that only on first click the popup must be opened, and not on any subsequent clicks, then do this
      shouldOpenWindow = !shouldOpenWindow;

   }
}
</script>
<body onclick="open_on_click()"></body>

Upvotes: 1

Related Questions