Jump to content

OnClick javaScript command


brian_donovan

Recommended Posts

hi, I am very new to javascripts, I hope this is no to easy or absurd and you will ignore it... pleas don't!

 

I am trying to fill out a form using applescripts and do javascript commands, and I have notice that all submit buttons include a tag called "OnClick". My question is.... Were is the code in the OnClick coming from?

 

for example take a look at this page:

 

 

 

http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_ev_onclick

 

 

 

in this page the button copy text activates a string called "copyText()"

 

Were is all the code? does it load at the beginning? is it in the server? how can I see and read this code? can I hijack this code and activate my one javascript once I made a few adjustments?

 

Thank you for your time!

 

 

Brian

Edited by brian_donovan
Link to comment
Share on other sites

Re-writing that example slightly...

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>title</title><script>window.onload = init;function init(){document.getElementById("btn1").onclick = copyText;}// end of functionfunction copyText() {var element1 = document.getElementById("field1");var element2 = document.getElementById("field2");var text1 = element1.value;element1.value = "";element2.value = text1;}// end of function</script></head><body>Field1: <input type="text" id="field1" value="Hello World!"><br>Field2: <input type="text" id="field2"><br><br><input type="button" id="btn1" value="Copy Text"/><p>A handler function is assigned to the button click event. The function moves the text from Field1 into Field2.</p></body></html>
Link to comment
Share on other sites

Were is all the code? does it load at the beginning?

It's a 1-line function defined in the head section of the HTML document:
<script>function copyText(){document.getElementById("field2").value=document.getElementById("field1").value;}</script>
Link to comment
Share on other sites

Were is all the code? does it load at the beginning? is it in the server?

it can be anywhere: directly written on the page or included in a separate script file loaded with the page. As Justsomeguy said, this particular piece of code is found in the head section of the page. Script code is usually placed in the head section as thats is often the most efficient place to put blocks of code, but they can be placed almost anywhere in the file. as for the server question... yes and no. the code originated from the server, but once the browser loads the code it doen't look back to what is on the server, until you reload the page

how can I see and read this code?

you can open up pretty much any development console that basically every major browser has. The hotkeys differ from browser to browser and OS to OS, but they all basically have the same capability. Google Chrome/Mozilla Firefox for instance is opened with [F12], Mac Safari opens up with [command+i], and I think IE 1st needs developmental tools activated then also opens with [F12].in this case the code is in easy, readable format, but some site like to minimize or gzip their code to make is smaller and take up less bandwith (letting pages load faster) the drawback to this is that the code becomes nigh unreadable for us humans

can I hijack this code and activate my one javascript once I made a few adjustments?

Of course! for example I can open up my chrome developers tab and go into the console and type simple javascript
alert("hello world"); 
press enter and the code runs immediately, rendering a pop-up on screen saying "hello world". You can overwrite entire functions inside the console this way. as to whether you should do this falls into a question of ethics and such. Theres some things many websites don't want you messing with. but its very useful in combinations with jquery to find thousands of elements and update them (then use the developer tab to copy the html and paste the result into a text editor, updating the file)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...