banner



How To Set Iframe Src Using Javascript

Change Iframe src with JavaScript

Cool tutorial on how to change the iframe src attribute value using JavaScript.

43424 views

d

By. Jacob

Edited: 2021-02-09 21:27

change src attribute value, JavaScript

In this tutorial, you can learn how to dynamically change the src attribute of an HTML iframe using JavaScript.

Usually you would not want to use an iframe, unless you actually need to load external content; some sites also prevent "framing" of their pages, in which case you will probably need to download the content you want to display, and then serve it up from your local server.

If you just want to create a DHTML page that loads content dynamically, you may want to use the appropriate HTML sectioning elements and then load the content by using a combination of AJAX and innerHTML or DOM manipulation.

To change the iframe src attribute, we will be using a select drop down menu, and a button element to activate the function that will change the src attribute. Note that this is only for example purposes, instead of using onClick you may want to consider using event handlers in an external script.

The below is a fully working example. You can copy and paste it into a .html file on your computer, and open it in your browser. The JavaScript itself is explained in the subsections of this article.

                        <!DOCTYPE html>            <            html            >            <            head            >            <            title            >Change src value of iframe dynamically</            title            >            <            script            type            =            "text/javascript"            >            function            newSrc            ()            {            var            e            =            document            .            getElementById            (            "MySelectMenu"            );            var            newSrc            =            e            .            options            [            e            .            selectedIndex            ].            value            ;            document            .            getElementById            (            "MyFrame"            ).            src            =            newSrc            ;            }            </            script            >            <            style            type            =            "text/css"            >            select            {            clear            :            both            ;}            </            style            >            </            head            >            <            body            >            <            iframe            src            =            "https://beamtic.com/Examples/ip.php"            style            =            "width:450px;height:450px;overflow:scroll;"            id            =            "MyFrame"            ></            iframe            >            <            select            id            =            "MySelectMenu"            >            <            option            value            =            "https://beamtic.com/Examples/ip.php"            >Show IP</            option            >            <            option            value            =            "https://beamtic.com/Examples/user-agent.php"            >Show User Agent</            option            >            </            select            >            <            button            onClick            =            "newSrc();"            >Change Iframe Src</            button            >            </            body            >            </            html            >          

Result:

Creating the Select Drop Down

The select menu consists of the select element, and multiple option elements, one for each site you want to allow in the iframe. I.e.

                            <              select              id              =              "MySelectMenu"              >              <              option              value              =              "http://yahoo.com/"              >Yahoo</              option              >              <              option              value              =              "http://www.google.com/custom"              >Google</              option              >              <              option              value              =              "https://beamtic.com/"              >Beamtic</              option              >              </              select              >            

You should place this somewhere in the body of your HTML – it does not really matter where you place it, as long as its in the body.

You can now select the different options from the drop down, but before it will work, you will also need a means of getting the value of the selected option. For this we will be making a custom JavaScript function that can be called via the onClick event on a button. Therefor, you should create a button element somewhere. I.e.

                            <              button              onClick              =              "newSrc();"              >Change Iframe Src</              button              >            

The JavaScript Code

The custom function that we will be using is really simple, it only has 3 lines of code!

                            function              newSrc              ()              {              var              e              =              document              .              getElementById              (              "MySelectMenu"              );              var              newSrc              =              e              .              options              [              e              .              selectedIndex              ].              value              ;              document              .              getElementById              (              "MyFrame"              ).              src              =              newSrc              ;              }            

The first line will create a reference to the select element that we will be using, for simplicity we doing it by ID in this tutorial.

The second line fetches the value of the currently selected option element, which will then be handed over to the iframe in the final and third line of code.

How to block framing of your own content

To block iframing of your own content, you should add the x-frame-options: SAMEORIGIN response header; if you are using an Apache server, this can either be done in your host configuration, or in a seperate .htaccess file by adding the following:

                            header              set x-frame-options SAMEORIGIN            
  1. We can not handle HTTP POST data from JavaScript, but we can still access the data while it is in the HTML form.

  2. getElementById should be used to select unique elements in a HTML document. Selected elements can be manipulated with JavaScript.

  3. Should you use querySelector or getElementById?; querySelector is more flexible, and able to perform more complex selections.

  4. the keydown event is not an ideal way to detect text-input by the user; instead you should consider using the input event.

  5. Easily check if an element is either hovered or in focus using plain JavaScript.

More in: JavaScript Tutorials

How To Set Iframe Src Using Javascript

Source: https://beamtic.com/change-iframe-src-javascript

Posted by: horndistrace.blogspot.com

0 Response to "How To Set Iframe Src Using Javascript"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel