Pull Url of the variable in window.location.href

Asked

Viewed 311 times

-1

I’m creating an Electron application where I have a field that the person inserts a url and when she clicks on the submit is directed to that url.

My html is like this:

<body>
    <form id="formulario">
        <div>
            <input type="text" value="Insert your URL" id="campo">
            <input onclick=Start() type="submit" value="Start">
        </div>
    </form>
</body>

Clicking pulls a function in the document config.js:

function Start() {    
    var InsertUrl = document.getElementById('campo').value;
    console.log(InsertUrl)
    window.location.href = + InsertUrl 
}

The value of url is being printed in the console.log, I just can’t pull the url into the window.location.href.

  • window.location.href = + InsertUrl this + is left there

2 answers

-2


var my_func = function(event) {
        var InsertUrl = document.getElementById('campo').value;
        console.log(InsertUrl)
        window.location.href = InsertUrl;
        event.preventDefault();
    };      

    var form = document.getElementById("formulario");
    form.addEventListener("submit", my_func, true);
<form id="formulario">
        <div>
            <input type="text" placeholder="Insert your URL"   id="campo">
            <input  type="submit" value="Start">
        </div>
</form>

Try this and see if it works, from a search on preventDefault()

  • He can’t read addeventlistener Uncaught Typeerror: Cannot read Property 'addeventlistener' of null at config.js:9 But thanks for the tip, I’ll search about.

-2

I did the above procedure, yet I didn’t pull the new page.

After that I saw that several people had the same problem.

What happens is that javascript loaded before the page DOM. The only change I made was in HTML:

<script type="text/javascript" src="./config.js" async></script>

I added async to load the script later and it worked perfectly.

Thank you so much for your help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.