1

Using jQuery or javascript, I have fumbled with this and finally give in.

How can I set an iframe src like this:

  1. If the window URL contains a parameter starting with "?sku="
  2. Cache entire parameter from ? to end (?sku=T235N&addressline=90803&form=product_search)
  3. 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!!!!

asked Sep 27, 2012 at 6:07
3
  • Use console.log(window.location) in Firebug or Chrome Console and take a look at what's in that object. Then use window.addEventListener/window.attachEvent to listen to page load and set the iframe's src property with what you find useful in the location object. Commented Sep 27, 2012 at 6:13
  • Jared thank you but I'm a jQuery rookie. How do I code that? Commented Sep 27, 2012 at 6:16
  • Take a look at this fiddle: jsfiddle.net/userdude/rUnQd/6 Note, it won't do anything since there's no sku to be found. But that should work in practice. Commented Sep 27, 2012 at 6:26

5 Answers 5

1

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
answered Sep 27, 2012 at 6:15
Sign up to request clarification or add additional context in comments.

1 Comment

that worked. It allowed me to target an iframe location from a form submit
1

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;
 }
 }
})();

http://jsfiddle.net/userdude/rUnQd/6/

answered Sep 27, 2012 at 6:29

6 Comments

Jared thank you. If I pass the param to the fiddle url it doesn't update the iframe source. jsfiddle.net/rUnQd/6/…
There's not really a simple way to get jsFiddle to mock that; the 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.
Jared, thank you again. I set that off jsFiddle but still not working - maybe I missed something here? digitalctn.com/Asicslocator/…
You're using an XHTML DOCTYPE, which means your 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.
Apparently, there was some junk at the end of the block too (maybe an encoding error). Anyhow, you don't need to put the ? 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/…
|
0

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);
answered Sep 27, 2012 at 6:17

Comments

0
<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"'
answered Sep 27, 2012 at 6:20

Comments

0

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

Joe
15.6k8 gold badges51 silver badges57 bronze badges
answered Sep 27, 2012 at 6:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.