2

I have a dropdown list like this:

<form name="change">
 <SELECT NAME="options" ONCHANGE="document.getElementById('frame1').src = this.options[this.selectedIndex].value">
  <option value="">Choose</option>
  <option value="stuff1.html">Stuff1</option>
  <option value="stuff2.html">Stuff2</option>
  <option value="stuff3.html">Stuff3</option>
 </SELECT>
</form>

Which loads a web page to an iframe by selecting one from the list:

<iframe class="iframe" id="frame1" src="" scrolling="yes" frameborder="0"></iframe>

Now I want it to load just a specific part of a web page which has an ID called cointainer1. All the pages have this part. Is this possible? I tried it by inserting #container1 after each URL, but the content of the iframe is scrollable so there are the other parts of the web page too. Changing it to not scrollable is not a solution for me. Any javascript solutions? Thanks in advance.

Adam
  • 47
  • 3

1 Answers1

1

There might be another solutions to do this, but I think you cat load Container content of each HTML page to your frame

follow these steps by JavaScript pure:

1- should make an function to load HTML page as XMLHttpRequest like:

 var getHTML = function (url, callback) { 
        if (!window.XMLHttpRequest) return;  
        var xhr = new XMLHttpRequest();  
        xhr.onload = function () {
            if (callback && typeof (callback) === 'function') {
                callback(this.responseXML);
            }
        } 
        xhr.open('GET', url);
        xhr.responseType = 'document';
        xhr.send();

    };

2- Make a function to handle your Option change event and call getHTML by passing your select value like:

  function loadFram() {
        var src = document.getElementById("opt1").value;
        getHTML(src, function (response) {
            var x = document.getElementById("frame1");
            var y = (x.contentWindow || x.contentDocument);
            if (y.document)y = y.document;
            y.body.innerHTML = response.documentElement.querySelector('[id="Container"]').innerHTML;
        });
    }

3- I supposed your HTML code is something like this or would be like this:

<form name="change">
    <SELECT id="opt1" NAME="options"  onchange="loadFram()"  >
        <option value="">Choose</option>
        <option value="stuff1.html">Stuff1</option>
        <option value="stuff2.html">Stuff2</option>
        <option value="stuff3.html">Stuff3</option>
    </SELECT>
</form>
<iframe class="iframe" id="frame1" src="" scrolling="yes"   frameborder="0"></iframe>

try it you see everything work nice.

but you can do it by JQuery by simplified steps by $.get or $.ajax like

 $('#opt1').change(function () {  
            var src = $(this).val(); 
            $.get(src, function (data) {
                var stuffContainer = document.createElement('div');
                stuffContainer.innerHTML = data;
                $('#frame1').contents().find('body').html($(stuffContainer).find('#Container').html());
            }); 
        }) ;

if you would like to use JQuery be sure to remove onchange="loadFram()" from your HTML.

Or by $.ajax like :

 $(function () {
            $('#opt1').change(function () { 
                var src = $(this).val();
                $.ajax({
                    url: src,
                    dataType: 'html',
                    success: function (response) {
                        var stuffContainer = document.createElement('div');
                        stuffContainer.innerHTML = response;
                        $('#frame1').contents().find('body').html($(stuffContainer).find('#Container').html());
                    }
                });
            });
        });

I preferred to provide the solution by JavaScript pure because you didn't Tag JQuery, anyway the above solutions works fine.

Aria
  • 3,724
  • 1
  • 20
  • 51
  • Thank you for your detailed answer, but neither of them works for me. Everything seems fine, but when I select from the options nothing happens. I have other JQuery codes that works just fine, so I don't really understand why this one doesn't. – Adam Dec 23 '17 at 18:01
  • What is the problem? can you debug it , or can you see the console log of your browser to check if there is error or not ?, I have tested it before posting it everything works like a charm, I think something went wrong with your code or HTML... – Aria Dec 24 '17 at 06:55
  • It says: "Failed to load xy/stuff1.html: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https." – Adam Dec 24 '17 at 11:21
  • Aha, that is another problem, because you are browsing your HTML file locally such as `E:\Proj\xy\stuff1.html` that error because of security reasons, but there is no problem on host, I was test it in Visual Studio and I have hosted my Web Proj on `Localhost:1368/stuff1.htm` for this reason I did not crossed with your mentioned problem, you should also host your web also. – Aria Dec 24 '17 at 11:29
  • See [THIS](https://stackoverflow.com/questions/10752055/cross-origin-requests-are-only-supported-for-http-error-when-loading-a-local) please. – Aria Dec 24 '17 at 11:34
  • I tried it. It doesn't say any error, but nothing happens. On the other hand if I try the pure JavaScript version it says: Uncaught ReferenceError: loadFram is not defined at HTMLSelectElement.onchange – Adam Dec 24 '17 at 11:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/161893/discussion-between-aria-and-adam). – Aria Dec 24 '17 at 11:51