0

I'm trying to run a javascript code via url to change values of the form in the page. Here is the content of the HTML page.

<html>
<body>
<form method='post'>
Name: <input type='text' id='name' value='' /><br />
Family: <input type='text' id='family' value='' /><br />
Email: <input type='text' id='email' value='' /><br />
<input type='submit' value='Submit' /><br />
</form>
</body>
</html>

Now, I enter the following value in the URL section of chrome:

javascript:document.getElementById('name').value = 'string1';

What happens is my whole page will go blank and only the value "string1" will be displayed in the page. However, running the following code works properly:

javascript:document.getElementById('name').value = 1;

It will change the value of the name field to 1. I'm wondering what is causing this problem.

Note: The purpose of this work is to inject code into webview in android.

Any advice is appreciated.

Hamed Afshar
  • 173
  • 2
  • 14
  • works fine in jsfiddle on chrome: https://jsfiddle.net/kscqdsw0/ – online Thomas Sep 24 '16 at 08:40
  • In firefox, chrome and android webview, it has the same issue. – Hamed Afshar Sep 24 '16 at 08:46
  • do you wait for document ready? because I think the error lies there. – online Thomas Sep 24 '16 at 09:02
  • The script works fine if I write it as a script tag in the html page. The issue is when I enter the code in the URL section of the chrome. You can check it here: http://hamed.afshar.ir/temp/acharge.php you open this url, when it loaded completely, you enter the following code in URL section of the browser and you see what happens: javascript:document.getElementById('name').value = 'string1'; – Hamed Afshar Sep 24 '16 at 09:03

3 Answers3

0

Try this.

javascript:document.getElementById('name').value = 'string1'; undefined;

Tack on a 'undefined;' to any script that returns a string.

Darrennchan8
  • 192
  • 4
  • 8
-1

That is because you are using single quotes after the attribute value

value=''

and you changing it's value in javascript using

document.getElementById('name').value='string1';

so the html becomes

<input type='text' id='name' value=''string1''

you can fix it using

value=""

or using

document.getElementById('name').value="string1";
shahryar
  • 83
  • 5
  • That didn't solve the problem. I changed the html to double quotes or script to double quotes, or both to double quotes, but all three failed and didn't solve the problem. – Hamed Afshar Sep 24 '16 at 10:40
-1

well i tried the following code

<html>
<body>
<form method='post'>
Name: <input type='text' id='name' value='' /><br />
Family: <input type='text' id='family' value='' /><br />
Email: <input type='text' id='email' value='' /><br />
<input type='submit' value='Submit' /><br />
</form>
<button onclick="change()">Click me</button>
<script>
function change(){
var obj=document.getElementById('name');
obj.value="string1";

}
</script>
</body>
</html>

and it works just fine the problem is in your script do you created a function to change the value or just putted the code in a script tag like this

<script>document.getElementById('name').value="String1";<script>
shahryar
  • 83
  • 5