如何在框架中执行 javascript?

发布于 2024-12-27 14:25:05 字数 1214 浏览 0 评论 0原文

我正在尝试在框架标记中编写 JavaScript 代码。但它没有执行我的 JavaScript 代码块。

我这样做的方式是:

<HTML>
<HEAD>
<TITLE>My HomePage</TITLE>
</HEAD>
<FRAMESET id="frameset1" rows="0,100%,0,0,0" frameborder=0>

<FRAME NAME="seat" SRC="seat/seat.html" frameborder="no" border=0  MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="no" FRAMEBORDER="no" NORESIZE>
<FRAME id="main" NAME="main" SRC="main.html" frameborder="no" border=0  MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="no" FRAMEBORDER="no" NORESIZE>

<script type='text/javascript'>
var myVar=1;
</script>

</FRAME>
<FRAME id="seatinfo" NAME="seatinfo" SRC="seatapi/seatinfo/seat_state.html" frameborder="no" border=0  MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="no" FRAMEBORDER="no" NORESIZE>
<FRAME id="blank" NAME="blank" SRC="blank.html" frameborder="no" border=0  MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="no" FRAMEBORDER="no" NORESIZE>   
<FRAME id="preloadHelp" NAME="preloadHelp" SRC="preloadHelp.html" frameborder="no" border=0  MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="no" FRAMEBORDER="no" NORESIZE> 

当我在 firebug 中检查 top.main.myVar 时,它显示未定义。

I am trying to write javascript code in a frame tag. But it is not excuting my block of javascript code.

The way i am doing this is :

<HTML>
<HEAD>
<TITLE>My HomePage</TITLE>
</HEAD>
<FRAMESET id="frameset1" rows="0,100%,0,0,0" frameborder=0>

<FRAME NAME="seat" SRC="seat/seat.html" frameborder="no" border=0  MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="no" FRAMEBORDER="no" NORESIZE>
<FRAME id="main" NAME="main" SRC="main.html" frameborder="no" border=0  MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="no" FRAMEBORDER="no" NORESIZE>

<script type='text/javascript'>
var myVar=1;
</script>

</FRAME>
<FRAME id="seatinfo" NAME="seatinfo" SRC="seatapi/seatinfo/seat_state.html" frameborder="no" border=0  MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="no" FRAMEBORDER="no" NORESIZE>
<FRAME id="blank" NAME="blank" SRC="blank.html" frameborder="no" border=0  MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="no" FRAMEBORDER="no" NORESIZE>   
<FRAME id="preloadHelp" NAME="preloadHelp" SRC="preloadHelp.html" frameborder="no" border=0  MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="no" FRAMEBORDER="no" NORESIZE> 

When i inspect top.main.myVar in firebug, it displays undefined.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花辞树 2025-01-03 14:25:05

要回答您的问题,请研究对 document.frames(或者在框架内工作时为parent.frame)。

话虽这么说,框架是不好的做法,恕我直言。我强烈建议您不要使用它们。

To answer your question, look into javascript calls to document.frames (or parent.frame in the case of working within a frame).

That being said frames are bad practice, IMHO. I would strongly encourage you to not use them.

寄居人 2025-01-03 14:25:05

您可以在目标框架中评估代码,而不是直接将代码放置在框架标记中,类似于 next

<FRAME id="main" NAME="main" SRC="main.html" frameborder="no" border=0  MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="no" FRAMEBORDER="no" NORESIZE></FRAME>

<script type='text/javascript'>
    window.frames.main.setTimeout(function(){
        // this code will run in the frame
        window.myVar=1; 
    },1);
</script>

在这种情况下 myVar 将在目标文档中可用。

Instead of placing code directly in frame tag, you can evaluate your code in target frame, something like next

<FRAME id="main" NAME="main" SRC="main.html" frameborder="no" border=0  MARGINWIDTH="0" MARGINHEIGHT="0" SCROLLING="no" FRAMEBORDER="no" NORESIZE></FRAME>

<script type='text/javascript'>
    window.frames.main.setTimeout(function(){
        // this code will run in the frame
        window.myVar=1; 
    },1);
</script>

in such case myVar will be available in the target document.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文