简体   繁体   中英

Open JavaScipt URL in new window

Wondering if anyone can help. I've created some code that depending on what option the user picks, they get taken to a specific URL. I have it all working fine, however, the URL won't open in a new window.

Any help would be brilliant.

This is the code:

 $(document).ready(function() { var selectVal1 = $("#selectBox1").val(); var selectVal2 = $("#selectBox2").val(); $("#selectBox1").change(function() { selectVal1 = $("#selectBox1 option:selected").val(); }); $("#selectBox2").change(function() { selectVal2 = $("#selectBox2 option:selected").val(); }); $("#click").click(function() { if(selectVal1 == 'A' && selectVal2 == 'A'){ location = "https://www.google.com/animalmanagement-25thaug", '_blank';} else if(selectVal1 == 'A' && selectVal2 == 'B'){ location = "https://www.google.com/animalmanagement-26thaug", '_blank';} if(selectVal1 == 'B' && selectVal2 == 'A'){ location = "https://www.google.com/artdesign-25thaug", '_blank';} else if(selectVal1 == 'B' && selectVal2 == 'B'){ location = "https://www.google.com/artdesign-26thaug", '_blank';} }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://www.sthelens.ac.uk/book-slot/book-enrolment.js"></script> <h3>Book Your Enrolment Slot</h3> <select id="selectBox1"> <option value="">Select which subject you would like to study?</option> <option value="A">Animal Management</option> <option value="B">Art &amp; Design</option> </select> <select id="selectBox2"> <option value="">Select what date you would like to come in and enrol</option> <option value="A">Tuesday 25th August 2020</option> <option value="B">Wednesday 26th August 2020</option> </select> <input id="click" type="button" value="Book Now" class="button">

window.open might help

window.open(<url>);

Or

window.open(<url>, "_blank"); // opens in new tab

You can use window.open(your url) ..

Or you can refer this doc.

https://developer.mozilla.org/en-US/docs/Web/API/Window/open

You can use code below

$("#click").click(function() {
    
   if(selectVal1 == 'A' && selectVal2 == 'A'){
       var  location = "https://www.google.com/animalmanagement-25thaug";
   } else if (selectVal1 == 'A' && selectVal2 == 'B'){        
       var location = "https://www.google.com/animalmanagement-26thaug";
   } else if (selectVal1 == 'B' && selectVal2 == 'A'){
       var location = "https://www.google.com/artdesign-25thaug";
   } else if (selectVal1 == 'B' && selectVal2 == 'B'){        
       var location = "https://www.google.com/artdesign-26thaug";
   }
   var wind = window.open(location, '_blank');
   //wind.focus(); // If required

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM