2011-10-14 160 views
0

我试图通过JavaScript打开一个窗口,但它只保持刷新无所事事。起初我以为这只是谷歌Chrome浏览器,但它在Firefox和IE中也是如此。不知道我的问题是什么。 JSFiddle说了一些关于“POST”的内容,但我不确定。建议?Javascript弹出框不会弹出

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); 

} 
+0

我喜欢这个标题 – Wazzzy

+0

这将是巨大的,如果你可以发表你的StackOverflow上的代码了。与jsfiddle的链接将来无法工作,比你的问题变得毫无用处。 – suknic

回答

0

我不知道这是否解决您的问题,而是你缺少在href哈希值。

尝试

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

,而不是

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

运气

1

你的问题是,你正在定义开放的 “窗口” 的范围。在JavaScript中定义的所有变量和函数都分配给窗口对象。下面有同样的效果:

var myVar = 10; 
window.myVar = 10; 

所以做这些:

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

所以你看,你的功能覆盖window.open和实际创建一个堆栈溢出。任何其他函数名称都应该起作用,如openWindow()

0

您使用一个名为“open()”的函数。由于没有确定的范围,这个功能被放在“窗口”范围(这意味着:您覆盖标准的“window.open()”函数

Renname你的功能,就可以工作了;)