我有一个网页,其中包含一些指向新scrren中打开的pdf文件的链接。如果我单击其中一个链接,新页面就会正常打开。如果我在该页面打开时单击另一个链接,它会将页面替换为新页面,这很好,但它会抛出一个JS错误的说“找不到成员”
HTML:
helpMenu.add( new AnchorMenuItem("User Guide", "javascript:openHelpWindow('../html/help/user_guide.html');") );
helpMenu.add( new AnchorMenuItem("FAQ", "javascript:openHelpWindow('../html/help/faq.html');") );
helpMenu.add( new AnchorMenuItem("Features", "javascript:openHelpWindow('../html/help/features.html');") );
helpMenu.add( new AnchorMenuItem("Overview", "javascript:openHelpWindow('../doc/Overview.pdf');") );
helpMenu.add( new AnchorMenuItem("Actual Info Guide", "javascript:openHelpWindow('../doc/ActualInfoGuide.pdf');") );
JS
/**
* Function to open the pop-up windows that the radio button
* or select box options will be chosen from.
*/
function openHelpWindow(url)
{
var w;
if (isBrowserNetscape)
{
// Netscape
w = window.open(url, "MSSTHelp", "resizable=yes, scrollbars=yes, menubar=no, location=no, toolbar=no, height=700, width=900, screenX=100, screenY=100");
}
else
{
// IE
w = window.open(url, "MSSTHelp", "resizable=yes, scrollbars=yes, menubar=no, location=no, toolbar=no, height=700, width=900, left=100, top=100");
}
w.focus();
}
错误发生在w.focus();
答案 0 :(得分:2)
可怕的旧代码。由于parms中的空格,它在编写的浏览器中会失败。这是文档window.open Best practices 无需区分顶部/左侧和screenX,screenY - 后者是Netscape 4及更早版本,如果您希望其他浏览器也可以指定,因为它们将忽略它们不需要的参数。任何parm = no都可以删除,任何parm = yes都可以设置为parm:
简易版:
function openHelpWindow(url) {
var w = window.open(url, "MSSTHelp",
"resizable,scrollbars,height=700,width=900,screenX=100,screenY=100,left=100,top=100");
if (w) w.focus();
}
根据上述文档重新使用已打开的窗口
var w; // global var
function openHelpWindow(url) {
if (!w || w.closed) {
w = window.open(url, "MSSTHelp",
"resizable,scrollbars,height=700,width=900,screenX=100,screenY=100,left=100,top=100");
}
else {
w.location=url; // OR window.open(url, "MSSTHelp");
}
if (w) w.focus();
}
答案 1 :(得分:0)
这是因为你的“w”变量的范围在你声明它的函数内。
为了在下次调用时可以访问它,您需要在函数外声明你的w var。当你调用这个函数时,它会创建一个新的box / inside /名为“w”的函数,它将引用粘贴到你用window.open()创建的窗口。然后,当函数完成执行时,它会抛出“w”。在函数范围之外声明“w”是/ better /,但仍然可能存在问题(因为现在你处于全局范围内)。
var w = null;
/**
* Function to open the pop-up windows that the radio button
* or select box options will be chosen from.
*/
function openHelpWindow(url)
{
if (isBrowserNetscape)
{
// Netscape
w = window.open(url, "MSSTHelp", "resizable=yes, scrollbars=yes, menubar=no, location=no, toolbar=no, height=700, width=900, screenX=100, screenY=100");
}
else
{
// IE
w = window.open(url, "MSSTHelp", "resizable=yes, scrollbars=yes, menubar=no, location=no, toolbar=no, height=700, width=900, left=100, top=100");
}
w.focus();
}
即使这样效率也不是很高,就好像窗口仍处于打开状态一样,当你可以简单地告诉你在第一时间打开的窗口转到新窗口时,你正在强制重建整个窗口。网址。
var w = null;
/**
* Function to open the pop-up windows that the radio button
* or select box options will be chosen from.
*/
function openHelpWindow(url)
{
if(w == null){
if (isBrowserNetscape)
{
// Netscape
w = window.open(url, "MSSTHelp", "resizable=yes, scrollbars=yes, menubar=no, location=no, toolbar=no, height=700, width=900, screenX=100, screenY=100");
}
else
{
// IE
w = window.open(url, "MSSTHelp", "resizable=yes, scrollbars=yes, menubar=no, location=no, toolbar=no, height=700, width=900, left=100, top=100");
}
} else {
w.document.location(url);
w.focus();
}
}
仍然效率不高!看看所有代码重复!
var w = null;
/**
* Function to open the pop-up windows that the radio button
* or select box options will be chosen from.
*/
function openHelpWindow(url)
{
if(w == null){
var winSpecs = "resizable=yes,scrollbars=yes,menubar=no,"
+ "location=no,toolbar=no,height=700,width=900,"
+ (isBrowserNetscape) ? "screenX=100,screenY=100" : "left=100,top=100";
w = window.open(url, "MSSTHelp", winSpecs);
} else {
w.location(url);
w.focus();
}
}
现在,请注意,即使在这个重构的代码中,我们也有一个问题......你确定除了Netscape之外的所有浏览器在打开这样的窗口时都使用“left”和“top”吗?除此之外,甚至还存在可用性和基于浏览器的弹出窗口阻止程序的问题,这可能会干扰代码的功能。