简体   繁体   English

Javascript弹出框不会弹出

[英]Javascript Popup Box Will Not Pop Up

I'm trying to open a window via javascript but it keeps just refreshing doing nothing. 我正在尝试通过javascript打开一个窗口,但它只是让人觉得无所事事。 At first I thought it was just Google Chrome but it did the same in firefox and IE. 起初我以为它只是谷歌Chrome,但它在firefox和IE中也是如此。 Not sure what my problem is. 不确定我的问题是什么。 JSFiddle says something about "POST" but I'm not sure. JSFiddle说“POST”,但我不确定。 Suggestions? 建议?

http://jsfiddle.net/uBwvx : 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);

}

Your problem is that you are defining open in the scope of "window". 您的问题是您在“窗口”范围内定义了open。 All variables and functions defined in JavaScript are assigned to the window object. JavaScript中定义的所有变量和函数都分配给window对象。 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. 所以你看,你的函数是覆盖window.open并实际创建堆栈溢出。 Any other function name should work, like openWindow() 任何其他函数名称都应该有效,比如openWindow()

dude change the name of your function to winopen: open is a keyword IM SURE OF IT: dude将你的函数名改为winopen:open是一个关键字IM SURE OF IT:

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

I am not sure if this fixes your problem, but you are missing a hash in the href. 我不确定这是否可以解决您的问题,但是您在href中缺少哈希值。

try 尝试

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

instead of 代替

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

luck 运气

You use a function called "open()". 您使用名为“open()”的函数。 Since there is no scope defined, this function is put in the "window" scope (which means: you overwrite the standard "window.open()" function. 由于没有定义范围,因此将此函数放在“窗口”范围内(这意味着:您将覆盖标准的“window.open()”函数。

Renname your function, everything should work ;) 命名你的功能,一切都应该工作;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM