猿问

重定向到不同的 URL 并加载 javascript 文件

我正在开发一个项目 - 当输入我的网站 ( www.mywebsite.com )的 URL 时,我希望它自动转到同一浏览器窗口中的另一个网站,然后转到另一个网站 ( www.anotherWebsiteOne.com )。 com),X秒后将在同一浏览器中加载另一个网站(www.anotherWebSiteTwo.com),依此类推。

我希望所有内容都保留在同一个浏览器窗口中

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>

   <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>


   <title>Basic Javascript Example</title>


   <script type="text/javascript" src="webSites.js"></script>

   

</head>


<body onload="start()">  


</body>


</html>

和我的 webSites.js:


var webSites = [

    'http://www.anotherWebPageOne.com/',

    'https://www.anotherWebPageTwo.com/',

    'http:www.anotherWebPageThree.com/',

];


var iTarget;


function nextTarget(){

    window.open( targets[iTarget], 'target' );

    if( ++iTarget >= targets.length ) {

        iTarget = 0;

    }

}


function start() {

    iTarget = 0;

    nextTarget();

    setInterval( nextTarget, 5000 );

}


start() 


慕沐林林
浏览 118回答 3
3回答

达令说

您可以使用 :<body onload="start()">&nbsp;&nbsp;</body>或者你可以简单地将 start() 添加到 js 文件的末尾

天涯尽头无女友

只需添加start()到 webSites.js 文件的末尾js 文件中的任何内容都会自动执行,您只需调用 js 文件中的 start 函数即可。另外,将您的 webSites 变量重命名为 Targets。

三国纷争

在你的 JS 文件中你只需要添加window.onload = <Your Function Name>;例如:-var webSites = [&nbsp; &nbsp; 'http://www.anotherWebPageOne.com/',&nbsp; &nbsp; 'https://www.anotherWebPageTwo.com/',&nbsp; &nbsp; 'http:www.anotherWebPageThree.com/',];var iTarget;function nextTarget(){&nbsp; &nbsp; window.open( targets[iTarget], 'target' );&nbsp; &nbsp; if( ++iTarget >= targets.length ) {&nbsp; &nbsp; &nbsp; &nbsp; iTarget = 0;&nbsp; &nbsp; }}window.onload = nextTarget;function start() {&nbsp; &nbsp; iTarget = 0;&nbsp; &nbsp; nextTarget();&nbsp; &nbsp; setInterval( nextTarget, 5000 );}我希望它能解决你的问题
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答