Search code examples
javascripthtmliframesrc

How can I insert a variable in iframe src to reach felixibilty for open the various sites? (by: html page and javascript )


I want my users to open their desired sites from my website, for this manner I need to insert a variable in src of iframe which change by user input strings.

Indeed I need a type of code like bellow:

<html>
    <body>
        <script type="text/javascript">
            var userInput_stringVariable= "this part is user desired input string" ;
            var adress= "https://en.wikipedia.org/wiki/" + userInput_stringVariable ;
        </script>
        <iframe src=adress width="100%" height="100%" frameborder="0"></iframe>
    </body>
</html>

This code doesn't work, while I need a code like this to work with!


Solution

  • A textfield where user can enter whatever they would like to search on wikipedia or whatever website you want, a submit button which will call a function after it is clicked. answer.value will give the value of textfield and it's concatenated with website initial url.

    HTML

    <input type="text" name="inputField" id="answer" placeholder="Enter what you wanna search">
    <button type="button" name="button" onclick="mySubmit()">Submit</button>
    <iframe id="search" src="" width="100%" height="100%" frameborder="0"></iframe>
    

    Script

    <script>
    function mySubmit() {
            let getInput = answer.value;
            var address;
            address = "https://en.wikipedia.org/wiki/" + getInput;
            document.getElementById('search').src = address;
    }
    </script>