简体   繁体   English

此JavaScript代码有什么问题?

[英]What is the problem in this JavaScript code?

var urlname= '/a/b.php?company_name='+company_name+'&series='+series;
document.getElementById('frame2').innerHTML='<IFRAME HEIGHT="600px" WIDTH="100%" NORESIZE="NORESIZE" SRC="'+urlname+'" NAME="aol" FRAMEBORDER="0" ALIGN="ABSBOTTOM" scrolling="no" id="a1" name="a1" onload="Javascript:heights('a1')"></IFRAME>';

I'm using this code but the function heights() is not running and it's not showing any error too. 我正在使用此代码,但是功能heights()没有运行,也没有显示任何错误。 What's the right syntax to call a function with arguments in JavaScript. 在JavaScript中使用参数调用函数的正确语法是什么? I'm new to it so don't have much idea. 我是新手,所以没有太多主意。 Any help would be highly appreciated. 任何帮助将不胜感激。

Where is the heights function located? 高度功能在哪里?

You need to escape the most inner quotes and remove "javascript:", eg replace onload="Javascript:heights('a1')" with onload="heights(\\'a1\\')" 您需要转义最内层的引号并删除“ javascript:”,例如,将onload="Javascript:heights('a1')"替换为onload="heights(\\'a1\\')"

Adding strings together is not as easy as you think. 将字符串加在一起并不像您想的那么容易。 You've got all sorts of problems here. 您这里遇到各种各样的问题。

var urlname= '/a/b.php?company_name='+company_name+'&series='+series; var urlname ='/a/b.php?company_name='+company_name+'&series='+series;

If company_name and series can have characters in that can't go in a URL parameter, like spaces or pluses or percents or ampersands or Unicode, this breaks. 如果company_name和series可以包含URL参数中不能包含的字符,例如空格或加号,百分号或&或Unicode,则此操作会中断。 They need encoding. 他们需要编码。

innerHTML='<IFRAME HEIGHT="600px" WIDTH="100%" NORESIZE="NORESIZE" innerHTML ='<IFRAME HEIGHT =“ 600px” WIDTH =“ 100%” NORESIZE =“ NORESIZE”

You can't use 'px' units in HTML, that's CSS. 您不能在HTML(CSS)中使用“ px”单位。 noresize isn't needed, you can't resize iframes anyway. 不需要noresize,无论如何您都无法调整iframe的大小。

SRC="'+urlname+'" SRC =“'+ urlname +'”

If urlname contains ", < or & you might have trouble. Needs to be HTML-encoded. 如果urlname包含“,<或&,则可能会遇到麻烦。需要进行HTML编码。

NAME="aol" FRAMEBORDER="0" ALIGN="ABSBOTTOM" scrolling="no" id="a1" name="a1" NAME =“ aol” FRAMEBORDER =“ 0” ALIGN =“ ABSBOTTOM” scrolling =“ no” id =“ a1” name =“ a1”

You've got two names? 你有两个名字吗? That's invalid and will confuse anything trying to use the window.frames array or getElementsByName. 这是无效的,并且会混淆任何尝试使用window.frames数组或getElementsByName的内容。

onload="Javascript:heights('a1')"> onload =“ Javascript:heights('a1')”>

' needs backslash-escaping since you used that as the string delimiter in your innerHTML='...' assignment. '需要反斜杠转义,因为您已将其用作innerHTML ='...'分配中的字符串定界符。

Don't begin an event handler with 'javascript:', that only makes sense in an href (and even then, javascript: URLs should never be used). 不要以'javascript:'开头事件处理程序,它仅在href中才有意义(即使如此,javascript:绝对不应使用URL)。

If you make your heights() function take an object instead of an id string, you can do away with all those names. 如果使heights()函数采用一个对象而不是id字符串,则可以删除所有这些名称。 And using DOM methods lets you avoid thinking about HTML-escaping. 使用DOM方法可以避免考虑HTML转义。 eg.: 例如。:

function heights() {
    alert(this.offsetHeight); // 'this' is the object the event was called on
}

var iframe= document.createElement('iframe');
iframe.frameBorder=iframe.scrolling= 'no';
iframe.style.height= '600px';
iframe.style.width= '100%';
iframe.onload= heights;
iframe.src= '/a/b.php?company_name='+encodeURIComponent(company_name)+'&series='+encodeURIComponent(series);
document.getElementById('frame2').appendChild(iframe);

check out with the url path you are given. 查看您提供的网址路径。 try like this. 尝试这样。

var urlname= './a/b.php?company_name='+company_name+'&series='+series;

And also escape the quotes 并且也逃避引号

Javascript:heights(\'a1\')

You don't need JavaScript:heights onload="" is already scoped to execute JavaScript. 您不需要JavaScript:heights onload =“”已经限定了执行JavaScript的范围。 You can try it with this: onload="alert('hello world')" 您可以使用以下方法尝试:onload =“ alert('hello world')”

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

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