Jump to content

Script-troubble


Hepolite

Recommended Posts

I have a code here, it's supposed to open some pages, but both buttons open exactly the same pages. Is it possible to name these scripts so they don't get involved with the other script? If so, how can I do that?And I'm supposed to have 6 of these at the same page, so I need to know how to solve this problem.Here's the code I'm using:

<script type="text/javascript">function verifyAnswer(){	if(confirm("Are you sure you want to do this?")){		if(confirm("Are you REALLY sure?")){			if(confirm("Are you REALLY REALLY sure?")){				if(confirm("This is the last chance to say no!")){					alert("Alright. You win. Press OK to continue.");					return true;				}else{					return false;				}			}else{				return false;			}		}else{			return false;		}	}else{		return false;	}}function open_win(){    // only run the window.open code if the verifyAnswer    // function returns true.    if(verifyAnswer())    {    window.open("http://something.com");    window.open("http://something.com");    }}</script><input type="submit" value="Somevalue" onclick="open_win()" /><script type="text/javascript">function verifyAnswer(){	if(confirm("Are you sure you want to do this?")){		if(confirm("Are you REALLY sure?")){			if(confirm("Are you REALLY REALLY sure?")){				if(confirm("This is the last chance to say no!")){					alert("Alright. You win. Press OK to continue.");					return true;				}else{					return false;				}			}else{				return false;			}		}else{			return false;		}	}else{		return false;	}}function open_win(){    // only run the window.open code if the verifyAnswer    // function returns true.    if(verifyAnswer())    {    window.open("http://anything.com");    window.open("http://anything.com");    }}</script><input type="submit" value="Anothervalue" onclick="open_win()" />

Link to comment
Share on other sites

Generally, you don't want to get into the practice of repeating code. If there is some function that is pretty much identical to another function where the only difference is the value of some variable, then pass that variable into the function. For example, in your open_win function:

function open_win(url){	// only run the window.open code if the verifyAnswer	// function returns true.	if(verifyAnswer())	{		window.open(url);		window.open(url);	}}

Link to comment
Share on other sites

I don't think I got what you mean.

function open_win(http://something.com){    // only run the window.open code if the verifyAnswer    // function returns true.    if(verifyAnswer())    {        window.open(http://something.com);        window.open(http://something.com);    }}

That didn't work :) ... Ain't it a way to name the scripts and use that name to indentify which script the user is asking for?

Link to comment
Share on other sites

If you set up the function as I posted earlier, then you could call it in your code like so:

<input type="submit" value="Somevalue" onclick="open_win('http://something.com/')" />

Link to comment
Share on other sites

I tried that. It didn't work...And I forgot to say: I have to make 6 or 12 buttons which will open a total of 600 links, and it would be best if they where all in the same page... The confirm boxes is there for giving some information, not for making one sure, as it looks like.

Link to comment
Share on other sites

What jesh is posting works fine, you must not be doing it correctly. You only want one open_win function, the one that jesh posted. You can have 10 links use the function to open 10 different sites if you want.<input type="submit" value="Somevalue" onclick="open_win('http://something.com/')" /><input type="submit" value="Somevalue" onclick="open_win('http://somethingelse.com/')" /><input type="submit" value="Somevalue" onclick="open_win('http://another.com/')" />Also, don't duplicate the verifyAnswer function, you only need to define it once.

Link to comment
Share on other sites

You're not understanding. That code does not send all buttons to the same site. If you look at the function that jesh posted, which I will paste again here:

function open_win(url){	// only run the window.open code if the verifyAnswer	// function returns true.	if(verifyAnswer())	{		window.open(url);		window.open(url);	}}

Notice that there is a function parameter called url. You can make that anything you want. These three buttons all use the same open_win function and they open different pages since they pass different urls to the function:<input type="submit" value="Somevalue" onclick="open_win('http://something.com/')" /><input type="submit" value="Somevalue" onclick="open_win('http://somethingelse.com/')" /><input type="submit" value="Somevalue" onclick="open_win('http://another.com/')" />Also, why do you have window.open twice in the function?

Link to comment
Share on other sites

Whops, I forgot to tell: ALL the buttons are supposed to go to the same site! Not six diffrent pages.And the buttons are supposed to open MORE than just one page. Every button shall open 50 or 100 pages. How can I do that?Here's my updated code:

<script type="text/javascript">function verifyAnswer(){	if(confirm("Sometext1")){		if(confirm("Sometext2")){			if(confirm("Sometext3")){				if(confirm("Sometext4")){					alert("Sometext5");					return true;				}else{					return false;				}			}else{				return false;			}		}else{			return false;		}	}else{		return false;	}}function open_win(url){    // only run the window.open code if the verifyAnswer    // function returns true.    if(verifyAnswer())    {        window.open(url);;    }}</script><input type="submit" value="Somevalue1" onclick="open_win('http://somepage.com/something/')" /><input type="submit" value="Somevalue2" onclick="open_win('http://somepage.com/somethingelse1/')" /><input type="submit" value="Somevalue3" onclick="open_win('http://somepage.com/somethingelse2/')" /><input type="submit" value="Somevalue4" onclick="open_win('http://somepage.com/somethingelse3/')" /><input type="submit" value="Somevalue5" onclick="open_win('http://somepage.com/somethingelse4/')" /><input type="submit" value="Somevalue6" onclick="open_win('http://somepage.com/somethingelse5/')" />

Also, why do you have window.open twice in the function?
Because I know allmost nothing about javascript. :)
Link to comment
Share on other sites

So you want a button that opens 100 windows. This may kill the client's computer by eating all of the memory, and will probably get caught by a lot of popup blockers who check for popup spamming, but you would need to call window.open once for each page you want to open and give each window a different name. The easiest way is to store all URLs in an array and loop through the array to open each one.

function spam_windows(){  var urls = new Array();  urls[0] = "http://address1.com";  urls[1] = "http://address2.com";  urls[2] = "http://address3.com";  urls[3] = "http://address4.com";  //etc  for (i = 0; i < urls.length; i++)	window.open(urls[i], "win" + i, "");}

Link to comment
Share on other sites

It's not very easy to do that. You just said you wanted one button to open 50 or 100 pages. If you want some sort of thing where they close one page and it opens another one, you will need to add onclose events to every page that you want that to happen on, and the onclose function on every page needs to know which page to open next. A lot of popup blockers block onclose events from opening pages though, that was a spam technique for a while.

Link to comment
Share on other sites

So when I click the button, it will take me to the page called "page2.html"? And if I place the code into that page again it will take me to "page3.html"? That won't work because I'm making a page that will do stuff in some other places (NOT BAD THINGS!!).Here's what the problem is, so everything is clear:1. I am NOT making spam! (Making a tool)2. I do not own the site these links lead to.3. When one window open, the previouse close.4. There are 50 links on each button.Any possibility of solving this?

Link to comment
Share on other sites

It would probably be a lot easier to build a frame-based player with your own navigation. Instead of just opening windows, one frame would contain a Javascript array of all the links in order, and you could have next and back buttons that would load a page from the array in the bottom frame. Then you don't have to deal with popup blockers at all.

Link to comment
Share on other sites

Yes, maybe that, but it would be really boring to wait for the page to load, then click next, wiat, click like 600 times... :) My thougth was that all you need should be one click, no matter how advanced the code should be... But if it ain't possible, I'll lay down the whole thing...

Link to comment
Share on other sites

You do one click. One window open, then it close and another open. Then that one close and another open, and so on. A total of 50 pages open and close.I have 12 buttons on one page, and all of these buttons open 50 pages, which close.The order would be like this:Window open. Window close.Another window open, then it close.Then one other window open, and it close....Last window close.That was what I thougth of.

Link to comment
Share on other sites

OK, that way you are going to have problems with popup blockers, because they block windows from opening when a page is closed. So, you can try to develop this, but you're always going to have to deal with popup blockers. Also, if the pages you are opening are on a different site that you don't control, you'll have additional problems because you will need to somehow attach the body onunload event to the other pages without access to them. You should be able to do that through Javascript, but it's another complexity. It would be much easier to build a frame-based player that you can use to scroll through the different pages. That way you can also go back, the way you want it they wouldn't be able to go back, they would have to start over and close 20 pages to get to the one they wanted to see.

Link to comment
Share on other sites

I would say that there is no point of going back on these pages, it's only possible to trigger the event on these pages once an hour. And it says exactly the same on every one of these pages, eccept the variable, which is one word... I can say that I'm creating some kind of tool. That's why I didn't use frame-based page.If there's nothing I can do about the popup blockers, I'll just leave it...But is a way to make that I said earlier? If so, what's the code? And how do I attach the body unonload element to this?

Link to comment
Share on other sites

I believe you can do something like this:

var win = window.open("page1.html");win.onUnload = function(){  var win2 = window.open("page2.html");  win2.onUnload = function()  {	var win3 = window.open("page3.html");	win3.onUnload = function()	{	  var win4 = window.open("page4.html");	  win4.onUnload = function()	  {		var win5 = window.open("page5.html");	  }	}  }}

That will set up 5 pages. You can see it gets a little messy, but I can't think of a better way to do it. If you have access to all of the pages, then you don't need to do it this way, you can just put some Javascript on each individual page to open another page when the window closes.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...