触发链接跳转的几种方式

链接跳转的几种方式

一、window.location.href方式跳转

var openUrl = 'http://www.baidu.com';
window.location.href = openUrl;

二、window.open方式跳转

var openUrl = 'http://www.baidu.com';
window.open(openUrl, '_blank');
// window.open(openUrl, '_self');

三、a标签方式跳转

代码如下(示例):

    // IOS和Android触发链接方式
	var openUrl = 'http://www.baidu.com';
    if (tempA) {
         document.body.removeChild(tempA);
     }
     var tempA= document.createElement('a');
     tempA.href = openUrl;
     tempA.style.display = 'none';
     console.log('创建a标签', tempA);
     tempA.click();

四、iframe标签方式跳转

代码如下(示例):

	// web浏览器通过iframe标签触发链接
	openUrl ='http://www.baidu.com';
	if (ifr) {
	    document.body.removeChild(ifr);
	}
	var ifr = document.createElement('iframe');
	ifr.src= openUrl;
	ifr.style.display = 'none';
	document.body.appendChild(ifr);

你可能感兴趣的:(JS,js)