Javascript弹出框不会弹出

发布于 2024-12-09 22:41:07 字数 2660 浏览 0 评论 0原文

我试图通过 javascript 打开一个窗口,但它只是不断刷新,什么也不做。起初我以为这只是 Google Chrome,但在 Firefox 和 IE 中也是如此。不确定我的问题是什么。 JSFiddle 说了一些关于“POST”的内容,但我不确定。建议?

http://jsfiddle.net/uBwvx

function romantic()
{
    document.body.bgColor = "pink";
    document.body.style.color = "red";
    document.images[1].src = "rom_main.jpg";

    // Searched online to find a script to override some styles. 
    // For loop with adding styles to each anchor didn't work for some reason. Kept being overriden somehow.
    var styleElement = document.createElement("style");
    styleElement.type = "text/css";
    if (styleElement.styleSheet) {
      styleElement.styleSheet.cssText = "a { color: red }";
    } else {
      styleElement.appendChild(document.createTextNode("a { color: red; }"));
    }
    document.getElementsByTagName("head")[0].appendChild(styleElement);
}

function adventure()
{
    document.body.bgColor = "#CDAA7D";
    document.body.style.color = "#5C3317";
    document.images[1].src = "adv_main.jpg";

    var styleElement = document.createElement("style");
    styleElement.type = "text/css";
    if (styleElement.styleSheet) {
      styleElement.styleSheet.cssText = "a { color: #5C4033 }";
    } else {
      styleElement.appendChild(document.createTextNode("a { color: #5C4033; }"));
    }
    document.getElementsByTagName("head")[0].appendChild(styleElement);
}

function relax()
{
    document.body.bgColor = "#B2DFEE";
    document.body.style.color = "#00688B";
    document.images[1].src = "rel_main.jpg";

    var styleElement = document.createElement("style");
    styleElement.type = "text/css";
    if (styleElement.styleSheet) {
      styleElement.styleSheet.cssText = "a { color: #000080 }";
    } else {
      styleElement.appendChild(document.createTextNode("a { color: #000080; }"));
    }
    document.getElementsByTagName("head")[0].appendChild(styleElement);
}

function family()
{
    document.body.bgColor = "#F0E68C";
    document.body.style.color = "#FFA54F";
    document.images[1].src = "fam_main.jpg";

    var styleElement = document.createElement("style");
    styleElement.type = "text/css";
    if (styleElement.styleSheet) {
      styleElement.styleSheet.cssText = "a { color: #6B4226 }";
    } else {
      styleElement.appendChild(document.createTextNode("a { color: #6B4226; }"));
    }
    document.getElementsByTagName("head")[0].appendChild(styleElement);
}

function open()
{
    mywindow = window.open("http://www.javascript-coder.com", "mywindow", "location=1,status=1,scrollbars=1,  width=100,height=100");
    mywindow.moveTo(0, 0);

}

I'm trying to open a window via javascript but it keeps just refreshing doing nothing. At first I thought it was just Google Chrome but it did the same in firefox and IE. Not sure what my problem is. JSFiddle says something about "POST" but I'm not sure. Suggestions?

http://jsfiddle.net/uBwvx:

function romantic()
{
    document.body.bgColor = "pink";
    document.body.style.color = "red";
    document.images[1].src = "rom_main.jpg";

    // Searched online to find a script to override some styles. 
    // For loop with adding styles to each anchor didn't work for some reason. Kept being overriden somehow.
    var styleElement = document.createElement("style");
    styleElement.type = "text/css";
    if (styleElement.styleSheet) {
      styleElement.styleSheet.cssText = "a { color: red }";
    } else {
      styleElement.appendChild(document.createTextNode("a { color: red; }"));
    }
    document.getElementsByTagName("head")[0].appendChild(styleElement);
}

function adventure()
{
    document.body.bgColor = "#CDAA7D";
    document.body.style.color = "#5C3317";
    document.images[1].src = "adv_main.jpg";

    var styleElement = document.createElement("style");
    styleElement.type = "text/css";
    if (styleElement.styleSheet) {
      styleElement.styleSheet.cssText = "a { color: #5C4033 }";
    } else {
      styleElement.appendChild(document.createTextNode("a { color: #5C4033; }"));
    }
    document.getElementsByTagName("head")[0].appendChild(styleElement);
}

function relax()
{
    document.body.bgColor = "#B2DFEE";
    document.body.style.color = "#00688B";
    document.images[1].src = "rel_main.jpg";

    var styleElement = document.createElement("style");
    styleElement.type = "text/css";
    if (styleElement.styleSheet) {
      styleElement.styleSheet.cssText = "a { color: #000080 }";
    } else {
      styleElement.appendChild(document.createTextNode("a { color: #000080; }"));
    }
    document.getElementsByTagName("head")[0].appendChild(styleElement);
}

function family()
{
    document.body.bgColor = "#F0E68C";
    document.body.style.color = "#FFA54F";
    document.images[1].src = "fam_main.jpg";

    var styleElement = document.createElement("style");
    styleElement.type = "text/css";
    if (styleElement.styleSheet) {
      styleElement.styleSheet.cssText = "a { color: #6B4226 }";
    } else {
      styleElement.appendChild(document.createTextNode("a { color: #6B4226; }"));
    }
    document.getElementsByTagName("head")[0].appendChild(styleElement);
}

function open()
{
    mywindow = window.open("http://www.javascript-coder.com", "mywindow", "location=1,status=1,scrollbars=1,  width=100,height=100");
    mywindow.moveTo(0, 0);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

坏尐絯 2024-12-16 22:41:07

伙计将你的函数名称更改为 winopen: open 是一个关键字 我确定:

http://jsfiddle.net /uBwvx/11/

dude change the name of your function to winopen: open is a keyword IM SURE OF IT:

http://jsfiddle.net/uBwvx/11/

如梦 2024-12-16 22:41:07

您的问题是您在“窗口”范围内定义打开。 JavaScript 中定义的所有变量和函数都分配给 window 对象。以下具有相同的效果:

var myVar = 10;
window.myVar = 10;

因此,请执行以下操作:

function open() { ... }
window.open = function() { ... }

所以您会看到,您的函数正在覆盖 window.open 并且实际上创建了堆栈溢出。任何其他函数名称都应该有效,例如 openWindow()

Your problem is that you are defining open in the scope of "window". All variables and functions defined in JavaScript are assigned to the window object. The following have the same effect:

var myVar = 10;
window.myVar = 10;

So do these:

function open() { ... }
window.open = function() { ... }

So you see, your function is overwriting window.open and actually creating an stack overflow. Any other function name should work, like openWindow()

暮年 2024-12-16 22:41:07

我不确定这是否可以解决您的问题,但您在 href 中缺少哈希值。

尝试

请求手册...

而不是

索取手册...

运气

I am not sure if this fixes your problem, but you are missing a hash in the href.

try

<a href="#" onclick="open()">Request A Brochure...</a>

instead of

<a href="" onclick="open()">Request A Brochure...</a>

luck

世俗缘 2024-12-16 22:41:07

您使用一个名为“open()”的函数。由于没有定义作用域,因此该函数被放置在“window”作用域中(这意味着:您覆盖标准的“window.open()”函数。

重命名您的函数,一切都应该可以工作;)

You use a function called "open()". Since there is no scope defined, this function is put in the "window" scope (which means: you overwrite the standard "window.open()" function.

Renname your function, everything should work ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文