1

I’m trying to change the src attribute of an <iframe> depending if the iframe src has /?foo or not.

I had this solution:

<script>
    jQuery(document).ready(function($) {
        $('iframe[src*="?foo"]').each(function() {
            $('#frame').attr('src', "http://www.example.com/page2");
        });
    });
</script>

<iframe id="frame" src=„www.example.com/page1/?foo"></iframe>

But my problem is that I have no access to the page, where the iframe is emded so I can't write this code into the <head> of the site.

I have access to both pages www.example.com/page1 and www.example.com/page2

I don’t know how I need to change the code to manipulate the src.. I am not sure if this is even possible, when I have no access to the page with the iframe

3
  • 1
    If the content of the iframe is on a different domain to the page which contains the iframe then you will be blocked from amending the DOM by the security features in place on the browser. There is no workaround for this. Commented Jul 4, 2016 at 14:26
  • You are using jQuery and javascript together. ....??? Commented Jul 4, 2016 at 14:27
  • Redirect to page2 on page1? Commented Jul 4, 2016 at 14:27

1 Answer 1

1

As seen in How to retrieve GET parameters from javascript? you can use the following code and call the function parseSecond("foo") to get the foo parameter on page1. You can find more information about this on the question page.

function parseSecond(val) {
    var result = "Not found",
        tmp = [];
    var items = location.search.substr(1).split("&");
    for (var index = 0; index < items.length; index++) {
        tmp = items[index].split("=");
        if (tmp[0] === val) result = decodeURIComponent(tmp[1]);
    }
    return result;
}

If you then want to redirect to page 2 you can write the following code to redirect the frame from page1 to page2 when the foo parameter has the value equal to bar.

if(parseSecond("foo")=="bar"){
    window.location="www.example.com/page2";
}
Sign up to request clarification or add additional context in comments.

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.