0

I have this script in JS which uses URL parameter for image location, it works fine when image is located in the same place as the script like below

<iframe src="pan.htm?image=img1.jpg" ></iframe>

But I need it to get the image from other location, how do I do it I tried the way below but it doesn't work this way (url is just an example)

<iframe src="pan.htm?image=http://someaddress.com/img1.jpg" ></iframe>
1

2 Answers 2

1

You need to encode the url. You can do this by running this script:

prompt('Here is your completed code:',encodeURIComponent(prompt('Enter the url to encode','')));

You can try it out here: http://jsfiddle.net/zenr8/

The encoded URL that the script gives you is the thing you need to enter into the URL parameter. In your case, the iframe would look like this:

<iframe src="pan.htm?image=http%3A%2F%2Fsomeaddress.com%2Fimg1.jpg" ></iframe>

If you need to enter something in an URL parameter from a script, you should always make sure that it's properly encoded. You can do this by using encodeURIComponent. If you then need to decode it for further usage in your script, just use decodeURIComponent again. For example:

alert(encodeURIComponent('http://google.com/?hl=en&q=search'));
//alerts http%3A%2F%2Fgoogle.com%2F%3Fhl%3Den%26q%3Dsearch
alert(decodeURIComponent('http%3A%2F%2Fgoogle.com%2F%3Fhl%3Den%26q%3Dsearch'));
//alerts http://google.com/?hl=en&q=search
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Guys this is what I needed, although there is also some kinde of corse support problem which should be resolved in the script I just have to fiddle around with it a bit to make it work.
1

You need to urlencode the parameter.

<iframe src="pan.htm?image=http%3A%2F%2Fsomeaddress.com%2Fimg1.jpg" ></iframe>

This could be done using php for example:

<iframe src="pan.htm?image=<?php echo urlencode("http://someaddress.com/img1.jpg"); ?>" ></iframe>

Or with javascript itself:

var iframe = document.createElement("iframe");
iframe.setAttribute("src","pan.htm?image="+encodeURIComponent("http://someaddress.com/img1.jpg"));
var wrapper = document.getElementById("iframe_wrapper");
wrapper.appendChild(iframe);

Comments

Your Answer

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.