最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url - Javascript - window.location.assign not working - Stack Overflow

programmeradmin0浏览0评论

I've a project where I scan a QR code, and it will then automatically print out information based on which code was scanned.

I have the scanner, and a printer. Each QR code is to correspond to a URL. I can get the scanner to fill an input field, and I can get an alert to show the information that was scanned. However, whenever I try to assign the URL it simply appends the information to the current URL, instead of replacing it.

I need it to replace the current URL. Does anyone know why it is doing this, and how to fix it?

<html>
<head><title>QR Printing</title></head>

<body onload="document.myform.mytextfield.focus();">

<form name="myform">
    <input type="text" name="mytextfield" onchange="checkForm()">
</form>

<script type="text/javascript" language="JavaScript">

function checkForm() { 
    var field1 = document.forms['myform'].elements['mytextfield'].value; 
    alert(field1);
    window.location.assign(field1);
}    

</script>

</body>
</html>

I've a project where I scan a QR code, and it will then automatically print out information based on which code was scanned.

I have the scanner, and a printer. Each QR code is to correspond to a URL. I can get the scanner to fill an input field, and I can get an alert to show the information that was scanned. However, whenever I try to assign the URL it simply appends the information to the current URL, instead of replacing it.

I need it to replace the current URL. Does anyone know why it is doing this, and how to fix it?

<html>
<head><title>QR Printing</title></head>

<body onload="document.myform.mytextfield.focus();">

<form name="myform">
    <input type="text" name="mytextfield" onchange="checkForm()">
</form>

<script type="text/javascript" language="JavaScript">

function checkForm() { 
    var field1 = document.forms['myform'].elements['mytextfield'].value; 
    alert(field1);
    window.location.assign(field1);
}    

</script>

</body>
</html>
Share Improve this question asked Nov 14, 2014 at 18:53 Andrew TerpeningAndrew Terpening 451 silver badge4 bronze badges 3
  • You can use <input autofocus /> instead of <body onload="something very long"> – Al.G. Commented Nov 14, 2014 at 18:55
  • Do you want to redirect the user to the new address or only to replace the text in the url-bar in the browser? – Al.G. Commented Nov 14, 2014 at 18:57
  • @Al.G. - I want to redirect based upon the URL that is supplied by the scanned QR code. – Andrew Terpening Commented Nov 14, 2014 at 19:06
Add a ment  | 

4 Answers 4

Reset to default 3

Looks like the form is submitting. Cancel it.

<form name="myform" onsubmit="return false">

You want:

window.location = field1;

Neither of the solutions worked for me in chrome browser. I have resolved this using:

setTimeout(function () { document.location.href = YourUrlLink }, 500);

Hope this helps who are seeking a solution for chrome browser issue.

add return false; after window.location.assign(field1);

 <html>
<head><title>QR Printing</title></head>

<body onload="document.myform.mytextfield.focus();">

<form name="myform">
    <input type="text" name="mytextfield" onchange="checkForm()">
</form>

<script type="text/javascript" language="JavaScript">

function checkForm() { 
    var field1 = document.forms['myform'].elements['mytextfield'].value; 
    alert(field1);
    window.location.assign(field1);
    return false;
}    

</script>

</body>
</html>
发布评论

评论列表(0)

  1. 暂无评论