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

kar
  • 27
  • 6
  • 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. – Rory McCrossan Jul 04 '16 at 14:26
  • You are using `jQuery` and `javascript` together. ....??? – Poonam Jul 04 '16 at 14:27
  • Redirect to `page2` on `page1`? – Teemu Jul 04 '16 at 14:27

1 Answers1

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";
}
Community
  • 1
  • 1
Paul Ghiran
  • 1,233
  • 1
  • 16
  • 29