ฉันต้องการมีปุ่มที่เปลี่ยนเส้นทางไปยัง URL ที่กำหนดและเปิดในแท็บใหม่ จะทำได้อย่างไร?
ฉันต้องการมีปุ่มที่เปลี่ยนเส้นทางไปยัง URL ที่กำหนดและเปิดในแท็บใหม่ จะทำได้อย่างไร?
คำตอบ:
ใช้สิ่งนี้:
<input type="button" value="button name" onclick="window.open('http://www.website.com/page')" />
ทำงานให้ฉันและจะเปิดหน้าต่าง 'ป๊อปอัป' ใหม่จริงแทนที่จะเป็นเบราว์เซอร์หรือแท็บใหม่ คุณยังสามารถเพิ่มตัวแปรเพื่อหยุดไม่ให้แสดงลักษณะเฉพาะของเบราว์เซอร์ได้ดังนี้:
onclick="window.open(this.href,'popUpWindow','height=400,width=600,left=10,top=10,,scrollbars=yes,menubar=no'); return false;"
ในจาวาสคริปต์คุณสามารถทำได้:
window.open(url, "_blank");
การเพิ่ม target = "_ blank" ควรทำดังนี้
<a id="myLink" href="www.google.com" target="_blank">google</a>
<a>แท็กจึงเหมาะสม ควรใช้รูปแบบเพื่อให้ดูเหมือนปุ่ม
คุณสามารถลืมการใช้ JavaScript ได้เนื่องจากเบราว์เซอร์ควบคุมว่าจะเปิดในแท็บใหม่หรือไม่ ตัวเลือกที่ดีที่สุดคือทำสิ่งต่อไปนี้แทน:
<form action="http://www.yoursite.com/dosomething" method="get" target="_blank">
<input name="dynamicParam1" type="text"/>
<input name="dynamicParam2" type="text" />
<input type="submit" value="submit" />
</form>
สิ่งนี้จะเปิดในแท็บใหม่เสมอไม่ว่าไคลเอนต์จะใช้เบราว์เซอร์ใดเนื่องจากไฟล์ target="_blank"แอตทริบิวต์
หากคุณต้องการเพียงแค่เปลี่ยนเส้นทางโดยไม่มีพารามิเตอร์ไดนามิกคุณสามารถใช้ลิงก์ที่มีtarget="_blank"แอตทริบิวต์ตามที่ Tim Bütheแนะนำ
ใช้window.openแทนwindow.locationการเปิดหน้าต่างหรือแท็บใหม่ (ขึ้นอยู่กับการตั้งค่าเบราว์เซอร์)
ซอของคุณใช้งานไม่ได้เนื่องจากไม่มีbuttonองค์ประกอบให้เลือก ลองinput[type=button]หรือให้ปุ่มและการใช้งานid#buttonId
วิธีที่ฉันต้องการมีข้อดีคือไม่มี JavaScript ฝังอยู่ในมาร์กอัปของคุณ:
CSS
a {
color: inherit;
text-decoration: none;
}
HTML
<a href="http://example.com" target="_blank"><input type="button" value="Link-button"></a>
เปิดในแท็บใหม่โดยใช้จาวาสคริปต์
<button onclick="window.open('https://www.our-url.com')" id="myButton"
class="btn request-callback" >Explore More </button>
ฉันเพิ่งใช้ target = "_ blank" ใต้แท็กแบบฟอร์มและทำงานได้ดีกับ FF และ Chrome ซึ่งจะเปิดในแท็กใหม่ แต่เมื่อใช้ IE จะเปิดในหน้าต่างใหม่
ลองดู
<a id="link" href="www.gmail.com" target="_blank" >gmail</a>
ลองใช้ HTML นี้:
<input type="button" value="Button_name" onclick="window.open('LINKADDRESS')"/>
คุณทำไม่ได้ ลักษณะการทำงานนี้ใช้ได้เฉพาะกับปลั๊กอินและผู้ใช้กำหนดค่าได้เท่านั้น
<BUTTON NAME='my_button' VALUE=sequence_no TYPE='SUBMIT' style="background-color:transparent ; border:none; color:blue;" onclick="this.form.target='_blank';return true;"><u>open new page</u></BUTTON>
ปุ่มนี้จะดูเหมือน URL และสามารถเปิดได้ในแท็บใหม่
aแท็กแทน
ใช้รหัสนี้
function openBackWindow(url,popName){
var popupWindow = window.open(url,popName,'scrollbars=1,height=650,width=1050');
if($.browser.msie){
popupWindow.blur();
window.focus();
}else{
blurPopunder();
}
};
function blurPopunder() {
var winBlankPopup = window.open("about:blank");
if (winBlankPopup) {
winBlankPopup.focus();
winBlankPopup.close()
}
};
มันทำงานได้ดีใน Mozilla, IE และ chrome และเวอร์ชันน้อยกว่า 22 แต่ใช้ไม่ได้ใน Opera และ Safari