Using jQuery or javascript, I have fumbled with this and finally give in.
How can I set an iframe src like this:
- If the window URL contains a parameter starting with "?sku="
- Cache entire parameter from ? to end (?sku=T235N&addressline=90803&form=product_search)
Change iframe src from /index.html to /default.html + parameter?
Change iframe src="/index.html to src="/default.html?sku=T235N&addressline=90803&form=product_search"
http://jsfiddle.net/Digitalctn/rUnQd/
Thank you a ton!!!!
5 Answers 5
i am not sure what have you tried with javascript on the above solution, even in fiddle i couldnt see any javascript
i would recommend you to go through the below posts
Using the GET parameter of a URL in JavaScript
the above post will help get the parameter "sku"
now if you have any value in the same you can set the iframe src by
document.getElementById("iframeid").src = "default.html?sku="+sku
1 Comment
First, give your iframe an id so we can getElementById:
<iframe src="http://hosted.where2getit.com/asics/index.html" id="sku_frame"
Then, setup a script to run on page load, detect if there's an sku= key in the location.search string, and grab the iframe reference and modify it's el.src:
(function load(){
if (window.addEventListener) {
window.addEventListener('load', run);
} else if (window.attachEvent) {
window.attachEvent('onload', run);
}
function run() {
var hassku = !!(location.search.indexOf('sku=') + 1);
if (hassku) {
document.getElementById('sku_frame').src = '/default.html' + location.search;
}
}
})();
6 Comments
window it's actually referencing is the iframe to the bottom, right, where the content displays. Which is not that simple to modify without jsFiddle freaking out. Also, note you're using a base relative URL, e.g., /default.html. You would need to compensate for that on jsFiddle, as well, since it will point to http://fiddle.jshell.net instead.script tags need to be <script type="text/javascript">//<![CDATA[...code...//]]></script>. Unless you have a good reason for using XHTML (other than "I use it to check for errors in markup"), do not use XHTML; use HTML5, which would be <!doctype html>. If you open that page with a console open, you'll see it's throwing an illegal character error on the space at the very end of the code block.? on default.html either; location.search already has it. Here it is "working" (the url loads an empty response). However, I show you the URL in the IFRAME src, and color the border blue so you can tell something's happened. jfcoder.com/t/… Here it is not working: jfcoder.com/t/… Just find the '?' in window location and set the src:
var url = window.location;
var pos = url.indexOf('?');
var query = url.substring(0,pos+1);
then set it with JQuery:
$('iframe').attr('src','/default.html?'+query);
Comments
<iframe id="changeIframe" src="http://hosted.where2getit.com/asics/index.html" frameborder="1" width="920" height="635" scrolling="no"></iframe>
document.getElementByid("changeIframe").src='http://hosted.where2getit.com/asics//default.html?sku=T235N&addressline=90803&form=product_search"'
Comments
You may try like this
$(function () {
var url = parseUrl(window.location.pathname).search;
if (url.match("sku").length > 0) {
$('#iframe1').attr('src', '/default.html' + url);
}
});
function parseUrl(url) {
var a = document.createElement('a');
a.href = url;
return a;
}
And the jsfiddle
console.log(window.location)in Firebug or Chrome Console and take a look at what's in that object. Then usewindow.addEventListener/window.attachEventto listen to page load and set theiframe'ssrcproperty with what you find useful in thelocationobject.skuto be found. But that should work in practice.