为什么我的按钮会这样做?

时间:2012-07-24 07:54:29

标签: javascript html function popup

然后转到未定义到test.com。

如何进入http://url.com然后http://test.com然后http://anotherdesiredurl.com

<head>

<script type="text/javascript">
        <!--
        function popup(url) {

            var width = 300;
            var height = 200;
            var left = (screen.width - width) / 2;
            var top = (screen.height - height) / 2;
            var params = 'width=' + width + ', height=' + height;
            params += ', top=' + top + ', left=' + left;
            params += ', directories=no';
            params += ', location=no';
            params += ', menubar=no';
            params += ', resizable=no';
            params += ', scrollbars=no';
            params += ', status=no';
            params += ', toolbar=no';
            newwin = window.open(url, 'popup', 'params');
            if (window.focus) {
                newwin.focus()
            }

            return false;

        }
setTimeout(function() {popup('http://www.test.com/'); }, 8000)


        //-->
        //]]>
    </script>


<head>
<body>
<input type="button" value="Click Blitch" onclick="popup();"/>
</body>

3 个答案:

答案 0 :(得分:1)

你写了

newwin = window.open(url, 'popup', 'params');

我原以为:

newwin = window.open(url, 'popup', params);

但我不确定它与你的pb有关

答案 1 :(得分:0)

因为你在这里没有将参数传递给popup函数:

<input type="button" value="Click Blitch" onclick="popup();"/>

你应该传递一个参数,或者检查函数是否未定义,然后分配一些URL

答案 2 :(得分:0)

试试这个:

<强>的JavaScript

function popup(url) {

    var width = 300;
    var height = 200;
    var left = (screen.width - width) / 2;
    var top = (screen.height - height) / 2;
    var params = 'width=' + width + ', height=' + height;          
    params += ', top=' + top + ', left=' + left;
    params += ', directories=no';
    params += ', location=no';
    params += ', menubar=no';
    params += ', resizable=no';
    params += ', scrollbars=no';
    params += ', status=no';
    params += ', toolbar=no';
    newwin = window.open(url, 'popup', 'params');
    if (window.focus) {
      newwin.focus()
    }

    return false;

  }

  function openWindows() {
    popup('http://url.com');
    popup('http://text.com');
    popup('http://http://anotherdesiredurl.com');
  }

<强> HTML

<input type="button" value="Click here" onclick="openWindows();"/>

如果要在超时后更改相同弹出窗口的位置,可以将JavaScript更改为:

  function popup(url, win) {

    var width = 300;
    var height = 200;
    var left = (screen.width - width) / 2;
    var top = (screen.height - height) / 2;
    var params = 'width=' + width + ', height=' + height;
    var newwin = win;
    params += ', top=' + top + ', left=' + left;
    params += ', directories=no';
    params += ', location=no';
    params += ', menubar=no';
    params += ', resizable=no';
    params += ', scrollbars=no';
    params += ', status=no';
    params += ', toolbar=no';
    if (newwin) {
      newwin.location = url;
    } else {
        newwin = window.open(url, 'popup', 'params');
    }
    if (window.focus) {
      newwin.focus()
    }

    return newwin;

  }

  function openWindows() {
    var win = popup('http://url.com');
    setTimeout(function () {
        popup('http://text.com', win);
    }, 2000);
    setTimeout(function () {
        popup('http://http://anotherdesiredurl.com', win);
    }, 4000);
  }