简体   繁体   English

jQuery Force为iframe设置src属性

[英]jQuery Force set src attribute for iframe

I have a main page (actually a JSP) with an iframe inside it as; 我有一个主页(实际上是一个JSP),其中包含一个iframe。

<iframe name="abc_frame" id="abc_frame" src="about:blank" frameborder="0" scrolling="no"></iframe>

Now there are multiple links on the main page (rendered dynamically via JSP) which can try to set the src to some URL... 现在主页上有多个链接(通过JSP动态呈现),可以尝试将src设置为某些URL ...

Now my question is,using jQuery (may be live()), can I override that in such a way that the "src" attribute for abc_frame would always have some particular value (eg "somefixedURL") 现在我的问题是,使用jQuery(可能是live()),我可以用abc_frame的“ src”属性始终具有某些特定值(例如“ somefixedURL”)的方式覆盖它吗?

I cannot capture the link clicking, as it is completely dynamically created via some Java code and hence I am trying to override the iframe src ? 我无法捕获链接单击,因为它是通过某些Java代码完全动态创建的,因此我试图覆盖iframe src吗?

Use attr 使用属性

$('#abc_frame').attr('src', url)

This way you can get and set every HTML tag attribute. 这样,您可以获得并设置每个HTML标签属性。 Note that there is also .prop() . 请注意,还有.prop() See .prop() vs .attr() about the differences. 有关差异,请参见.prop()与.attr() Short version: .attr() is used for attributes as they are written in HTML source code and .prop() is for all that JavaScript attached to the DOM element. 简短版本: .attr()用于属性,因为它们是用HTML源代码编写的,而.prop()用于附加到DOM元素的所有JavaScript。

if you are using jQuery 1.6 and up, you want to use .prop() rather than .attr(): 如果您使用的是jQuery 1.6及更高版本, 则要使用.prop()而不是.attr():

$('#abc_frame').prop('src', url)

See this question for an explanation of the differences. 有关差异的说明,请参见此问题

While generating the links set the target to the iframes name property and you probably wont have to deal with jquery at all. 在生成链接时,将目标设置为iframes name属性,您可能根本不必处理jquery。

<a href="inventory.aspx" target="contentframe"  title="Title Inventory">
<iframe id="iframe1" name="contentframe"  ></iframe>
$(document).ready(function() {
    $('#abc_frame').attr('src',url);
})

Setting src attribute didn't work for me. 设置src属性对我不起作用。 The iframe didn't display the url. iframe没有显示网址。

What worked for me was: 对我有用的是:

window.open(url, "nameof_iframe");

Hope it helps someone. 希望它可以帮助某人。

<script type="text/javascript">
  document.write(
    "<iframe id='myframe' src='http://www.example.com/" + 
    window.location.search + "' height='100' width='100%' >"
  ) 
</script>  

This code takes the url-parameters (?a=1&b=2) from the page containing the iframe and attaches them to the base url of the iframe. 此代码从包含iframe的页面获取url参数(?a = 1&b = 2),并将它们附加到iframe的基本url。 It works for my purposes. 它适用于我的目的。

$(".excel").click(function () {
    var t = $(this).closest(".tblGrid").attr("id");
    window.frames["Iframe" + t].document.location.href = pagename + "?tbl=" + t;
});

this is what i use, no jquery needed for this. 这就是我用的,不需要jQuery。 in this particular scenario for each table i have with an excel export icon this forces the iframe attached to that table to load the same page with a variable in the Query String that the page looks for, and if found response writes out a stream with an excel mimetype and includes the data for that table. 在这种特殊情况下,对于我拥有的每个表都有一个excel导出图标,这会强制连接到该表的iframe使用该页面所要查找的查询字符串中的变量加载同一页面,并且如果找到响应,则会用excel mimetype并包含该表的数据。

You cannot set FIX iframe's src or prevent javascript/form submit to change its location. 您无法设置FIX iframe的src或阻止javascript / form提交更改其位置。 However you can put script to onload of the page and change action of each dynamic link. 但是,您可以将脚本放入页面的加载中,并更改每个动态链接的操作。

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

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