copenndthagen
copenndthagen

Reputation: 50790

jQuery Force set src attribute for iframe

I have a main page (actually a JSP) with an iframe inside it as;

<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...

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 (e.g. "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 ?

Upvotes: 92

Views: 200970

Answers (8)

Hari
Hari

Reputation: 277

Setting src attribute didn't work for me. The iframe didn't display the url.

What worked for me was:

window.open(url, "nameof_iframe");

Hope it helps someone.

Upvotes: 8

PiTheNumber
PiTheNumber

Reputation: 23562

Use attr

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

This way you can get and set every HTML tag attribute. Note that there is also .prop(). See .prop() vs .attr() about the differences. 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.

Upvotes: 185

Arend
Arend

Reputation: 333

<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. It works for my purposes.

Upvotes: 3

vernmic
vernmic

Reputation: 25

$(".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. 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.

Upvotes: 1

Brian Rizo
Brian Rizo

Reputation: 858

While generating the links set the target to the iframes name property and you probably wont have to deal with jquery at all.

<a href="inventory.aspx" target="contentframe"  title="Title Inventory">
<iframe id="iframe1" name="contentframe"  ></iframe>

Upvotes: 9

kalyfe
kalyfe

Reputation: 1005

if you are using jQuery 1.6 and up, you want to use .prop() rather than .attr():

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

See this question for an explanation of the differences.

Upvotes: 21

Jan Pfeifer
Jan Pfeifer

Reputation: 2861

You cannot set FIX iframe's src or prevent javascript/form submit to change its location. However you can put script to onload of the page and change action of each dynamic link.

Upvotes: 1

Jayendra
Jayendra

Reputation: 52809

$(document).ready(function() {
    $('#abc_frame').attr('src',url);
})

Upvotes: 8

Related Questions