从iframe标签内调用JS函数 [英] Calling JS function from within iframe tag

查看:93
本文介绍了从iframe标签内调用JS函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Javascript和html的新手,我正在尝试制作以下

代码段工作,但事实并非如此。它拒绝调用getPage()

函数,我总是得到404错误。我知道代码的格式是

,因为iframe部分和html代码的其他部分一样正常工作

。但是

它没有在getPage()函数中执行任何代码,因为有些代码是

" hello world"在getPage()中写入不会打印。


--------------------------- -------------------------------------------------- ------

..

..

..


< script language =" javascript" type =" text / javascript">


函数getPage(){

<! -

.. ..这里的一些代码

返回pageURL;

// - >

}

< /脚本>

< / head>


< body>

< iframe src =" getPage()" ;名称= QUOT;凡是" width = 100%height = 600

frameborder =" 0"滚动= QUOT;是"> < / iframe>

..

..

..

------- -------------------------------------------------- ----------------------


任何人都可以帮助解决语法错误,或者我在尝试什么

做不可能吗?谢谢,请不要火焰。

I''m new to Javascript and to html and am trying to make the following
code snippet work but it doesn''t. It refuses to call the getPage()
function and I always get a 404 error. I know the code is getting as
far as the iframe section as other parts of the html code work
correctly. But
it is not executing any code within the getPage() function as some
"hello world" writes within getPage() wouldn''t print.

-----------------------------------------------------------------------------------
..
..
..

<script language="javascript" type="text/javascript">

function getPage() {
<!--
.... some code here
return pageURL;
//-->
}
</script>
</head>

<body>
<iframe src="getPage()" name="whatever" width=100% height=600
frameborder="0" scrolling="yes"> </iframe>
..
..
..
-------------------------------------------------------------------------------

Can anyone help what the syntax error is here, or what I am trying
to do is not possible? Thanks and please no flames.

推荐答案



ukrbend写道:

ukrbend wrote:
我是Javascript和html的新手,我正在努力制作以下
代码片段,但事实并非如此。它拒绝调用getPage()
函数,我总是得到404错误。我知道代码正在像iframe部分一样,正如html代码的其他部分正常工作一样。但是它没有在getPage()函数中执行任何代码,因为有些
hello world在getPage()中写入将无法打印。

------------------------------- -------------------------------------------------- -




< script language =" javascript"类型= QUOT;文本/ JavaScript的">


该语言已被弃用且不必要。 type属性将

足够。

函数getPage(){
<! -
[...] // - > ;


为什么你的函数里面有html注释标签?那是

肯定是一个错误。立即删除它们。

< iframe src =" getPage()">< / iframe>
I''m new to Javascript and to html and am trying to make the following
code snippet work but it doesn''t. It refuses to call the getPage()
function and I always get a 404 error. I know the code is getting as
far as the iframe section as other parts of the html code work
correctly. But
it is not executing any code within the getPage() function as some
"hello world" writes within getPage() wouldn''t print.

-----------------------------------------------------------------------------------
.
.
.

<script language="javascript" type="text/javascript">
The language is deprecated and unnecessary. The type attribute will
suffice.
function getPage() {
<!-- [...] //-->
Why do you have html comment tags inside your function? That is
definitely an error. Remove those immediately.
<iframe src="getPage()"></iframe>




您输错了使用src属性。 src

属性的值应该是要在iframe中显示的文档的URL。


请尝试以下内容,因为您刚刚开始:


修复上述错误后,创建第二个html,将其命名为

pageTwo.html。然后,将您的javascript移动到pageTwo.html。要

调用函数调用,请将以下内容放在正文中:


< script type =" text / javascript">

getPage();

< / script>

最后,对于pageOne.html中的iframe,更改值

src如下:


< iframe src =" pageTwo.html">< / iframe>



You are incorrectly using the src attribute. The value of the src
attribute should be the URL of the document to show in the iframe.

Try the following instead since you are just beginning:

After you fix the above mentioned errors, create a second html, call it
pageTwo.html. Afterwards, move your javascript into pageTwo.html. To
invoke the function call, place the following somewhere in the body:

<script type = "text/javascript">
getPage();
</script>

And finally, for your iframe in your pageOne.html, change the value of
the src to be as follows:

<iframe src = "pageTwo.html"></iframe>


感谢您的回复。关于你的一些观点,为什么html

评论标签呢?他们在那里,以防用户有一个不支持javascript的旧的

浏览器。这方面的几个例子可以在这里找到:


http://www.prestwood.com/community/w...ns/beg_js.html
http://www.engin.umd.umich.edu/ CIS / c ... pt / hellow.html
http://www.webreference.com/js/scripts/basic_date/

其次,你说src属性应该是<的文件的URL br />
显示在iframe中。这正是getPage()函数

返回的内容。我知道getPage()工作正常,因为我试过调用

它不在iframe标签中。在我看来,两件事之一可能是错误的。一,html不允许你调用javascript函数

如果它是在属性值的上下文中。或者两个,我在调用

函数的方式中引入了一些

语法错误。我尝试了几种变体(即没有引号,单个

引号等等),但到目前为止还没有碰到它。

但是感谢帮助,我会尝试你的解决方案。

Thanks for the reply. Regarding some of your points, why are the html
comment tags there? They are there in case the user has an older
browser that doesn''t support javascript. Several examples of this can
be found here:

http://www.prestwood.com/community/w...ns/beg_js.html
http://www.engin.umd.umich.edu/CIS/c...pt/hellow.html
http://www.webreference.com/js/scripts/basic_date/

Second, you said the src attribute should be the URL of the document to
show in the iframe. That is exactly what the getPage() function
returns. I know it getPage() works correctly since I''ve tried calling
it not in the iframe tag. It seems to me one of two things can be
wrong. One, that html doesn''t allow you to call a javascript function
if it''s in the context of a value of a property. Or two, there is some
syntax error that I am introducing in the way I am calling the
function. I have tried several variation (i.e. no quotes, single
quotes, etc...), but so far haven''t hit upon it.
But thanks for the help, I will try your solution.


找到答案,应该是这样的:


< iframe src =" javascript :getPage()" .....

Found the answer, it should be this:

<iframe src="javascript:getPage()" .....


这篇关于从iframe标签内调用JS函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆