...

View Full Version : Using checkboxes to open links in new windows.



xenoche
07-25-2009, 06:39 AM
Hi all!

Javascript newbie here. I am trying to setup a javascript code that will open links to items that are checked. Is this possible by client side without using CGI?

Philip M
07-25-2009, 08:41 AM
Checkboxes are not very suitable for this as the user can select multiple boxes. Prefer to use radio buttons:-




<form name= "myform">

Google <input type = "radio" name = "rad1" onclick = "go()">
Coding Forums <input type = "radio" name = "rad1" onclick = "go()">
JavaScriptKit <input type = "radio" name = "rad1" onclick = "go()">

</form>

<script type = "text/javascript">

function go() {
var sel;
for (var i =0; i<document.myform.rad1.length; i++) {
if (document.myform.rad1[i].checked) {
sel = i;
}
}
if (sel == 0) {window.location = "http://www.google.com"}
if (sel == 1) {window.location = "http://www.codingforums.com"}
if (sel == 2) {window.location = "http://www.javascriptkit.com"}
}
</script>


Quizmaster: What is a 12-sided solid figure called?
Contestant: Well, I know that ten sides is a hexagon. So I'll say an octogon.

xenoche
07-26-2009, 07:31 PM
Checkboxes are not very suitable for this as the user can select multiple boxes. Prefer to use radio buttons:-


Thanks for responding. But the whole point is for the user to HAVE the option to select multiple links, I need it to launch all the links selected by the user.

Philip M
07-26-2009, 08:18 PM
Thanks for responding. But the whole point is for the user to HAVE the option to select multiple links, I need it to launch all the links selected by the user.

What, all at once?


<form name= "myform">
Google <input type = "checkbox" name = "chk1">
Coding Forums <input type = "checkbox" name = "chk2">
JavaScriptKit <input type = "checkbox" name = "chk3">
<br><br>
<input type = "button" name = "but1" value = "Launch selected URLs" onclick = "go()">
</form>

<script type = "text/javascript">

function go() {
if (document.myform.chk1.checked == true) {window.open("http://www.google.com","window1")}
if (document.myform.chk2.checked == true) {window.open("http://www.codingforums.com","window2")}
if (document.myform.chk3.checked == true) {window.open ("http://www.javascriptkit.com","window3")}
}

</script>

You can add attributes to each window, such as width, height, resizable, scrollbars, toolbar etc.

Example:-

if (document.myform.chk1.checked == true) {window.open("http://www.google.com","window1",'width=800,height=600,scrollbars=yes,resizable=no,toolbar=yes')}

xenoche
07-26-2009, 11:06 PM
Thank you very much for your help, Phil. Much appreciated. Let me try this code out when I get home from work.

Thanks again!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum