使用 javascript 重新加载 iframe 时将变量传递给 jsp

发布于 2024-08-30 20:15:20 字数 747 浏览 2 评论 0 原文

在我的 javascript 代码中,我在 iframe 中加载 jsp 页面并传递一些变量,如下所示:

htmlData[i++]="";

然后我必须在执行操作后重新加载该 jsp 页面,我这样做:

accord = ui.newHeader.text(); document.getElementById('mapframe').contentWindow.location.reload();

重新加载有效,只是“accord”变量没有更新。当我从jsp中调用它时,它仍然具有原来的值。重新加载 iframe/jsp 时如何传递新值?

它不应该有任何区别,但我正在使用 jquery,这是针对 Yahoo zimlet 的。

谢谢。

In my javascript code I load a jsp page in an iframe and pass some variables like this:

htmlData[i++]="<iframe id=\"mapframe\" frameborder=\"0\" style=\"width:330px;height:300px\" src=\"" + "mapURL.jsp"
+"&lat=" + AjxStringUtil.urlComponentEncode("33.65")
+"&lng=" + AjxStringUtil.urlComponentEncode("-84.42")
+"&accord=" + AjxStringUtil.urlComponentEncode(accord)
+"\"></iframe>";

Then I have to reload that jsp page after an action, and I do this:

accord = ui.newHeader.text();
document.getElementById('mapframe').contentWindow.location.reload();

The reload works except that the "accord" variable is not getting updated. When I call it from the jsp, it still has its original value. How do I pass the new value when reloading the iframe/jsp?

It shouldn't make any difference, but I am working with jquery and this is for a Yahoo zimlet.

Thanks.

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

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

发布评论

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

评论(1

一笑百媚生 2024-09-06 20:15:20

“accord”变量如何“更新”?目前,您只是重新加载第一次构建的相同 URL;如果 JavaScript accord 变量已更改,并且您想要更改 URL 以反映这一情况,则必须构建一个新 URL 并将 iframe 导航到具有不同 accord 的新页面范围。

function encodeParameters(o) {
    var s= [];
    for (var k in o)
        if (o.hasOwnProperty(k))
            s.push(encodeURIComponent(k)+'='+encodeURIComponent(o[k]));
    return s.join('&');
}

var accord= ui.newHeader.text(); 
var url= 'mapURL.jsp?'+encodeParameters({
    lat: '33.65', lng: '-84.42', accord: accord
});
document.getElementById('mapframe').src= url;

注意 contentWindow 历来是 IE 扩展,最好避免使用。此外,如果您必须通过将 HTML 粘合在一起来创建 iframe,则需要先对 URL 进行 HTML 转义,然后再将其包含在标记中。否则,URL 中的 & 字符无效,并且当您输入与 HTML 实体名称匹配的参数名称时,可能会导致问题。

function encodeHTML(s) {
    return s.replace(/&/g, '&').replace(/</g, '<').replace(/"/g, '"');
}

htmlData[i++]= '<iframe id="mapframe" src="'+encodeHTML(url)+'" frameborder="0" style="width:330px;height:300px"></iframe>';

(我通常更喜欢使用 DOM 方法创建元素,以避免显式 HTML 编码。)

How is the ‘accord’ variable getting ‘updated’? At the moment you are merely reloading the same URL you built the first time round; if the JavaScript accord variable has changed and you want to change the URL to reflect that, you must build a new URL and navigate the iframe to the new page with the different accord parameter.

function encodeParameters(o) {
    var s= [];
    for (var k in o)
        if (o.hasOwnProperty(k))
            s.push(encodeURIComponent(k)+'='+encodeURIComponent(o[k]));
    return s.join('&');
}

var accord= ui.newHeader.text(); 
var url= 'mapURL.jsp?'+encodeParameters({
    lat: '33.65', lng: '-84.42', accord: accord
});
document.getElementById('mapframe').src= url;

Note contentWindow is historically an IE extension, and better avoided. Also if you must create your iframe by sticking HTML together, you will need to HTML-escape the URL before including it in the markup. Otherwise the & characters in the URL are invalid and likely to cause trouble when you hit a parameter name that matches an HTML entity name.

function encodeHTML(s) {
    return s.replace(/&/g, '&').replace(/</g, '<').replace(/"/g, '"');
}

htmlData[i++]= '<iframe id="mapframe" src="'+encodeHTML(url)+'" frameborder="0" style="width:330px;height:300px"></iframe>';

(I'd generally prefer to create elements using DOM methods, to avoid having to explicitly HTML-encode.)

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