Question:
How to use Java Script.?
steve
2010-12-02 22:58:01 UTC
I am a computer science major but have never really learned javascript and I have a few questions about what it can and cannot do.

Firstly, it is possible to run my own javascript on someone else's website. For example could I run my javascipt on a page facebook?

And secondly, what got me thinking about this is I have been playing snake on facebook. Would it be possible to write script that would just push an arrow key every so often to play the snake game on its own. If this is possible then the logic would be pretty simple.

Thanks!
Four answers:
brisray
2010-12-03 00:47:40 UTC
When you type JavaScript into a browser address bar it doesn't really interact with the webpage that's already there but with the browser itself.



javascript:this.window.close(); - really does try and close the browser window

javascript:this. window. location. href=" http://microsoft.com "; - does take you to microsoft.com



(remove all the spaces in the above line - Yahoo truncates it when I posted it.



If you try to interact with the webpage it doesn't work so well. If a webpage contains an element with an ID of doorknob then javascript: document.getElementById( 'doorknob') .innerHTML = 'Fred Flinstone'; clears the entire page and "Fred Flintstone" appears on the screen. Again, remove all the spaces to get this to work (or not).



If the webpage does not contain the ID doorknob then you just get an error.



There's a lot of discussion about JavaScript simulating keystrokes - http://www.google.com/search?q=javascript+simulate+keystrokes - luckily it can't do this apart from the page it's on and not to outside the browser. Imagine what would happen if JavaScript could simulate the Ctrl, Alt and Del keys outside the browser!



So to answer your questions...



1) JavaScript from one page can interact with another but only if they are related. parent/daughter/passed by querystring. There was a method of JavaScript cross site scripting (XSS) but that's largely a thing of the past now so no, you can't run your code on Facebook. Besides which it was mostly just malicious links, not actually controlling one site from another.

'

2) As most of these games are written in Flash then no, you aren't going to be able to do that. There are games completely written with JavaScript but you're going to spend a lot of time examining the code and working out how to hook into it,then you have to type the code into the address bar, one line at a time. You may as well just press the keys and play the game properly.
2010-12-02 23:03:05 UTC
The defacto site for web programming seems to be w3schools.com.

They have also written a new book about Javascript if you prefer the paper version.

You can find it on Amazon for about $19. There is also an HTML book.



Javascript runs in any web browser without compiling it, but I think some web sites can block it so you can't do any damage.



I'm not sure if it can "push" keys on the keyboard, but I know there is a timer so that certain things can happen, but I'm not sure about things like keystrokes. That might be something more like regular Java (which isn't related to Javascript if you didn't already know. Despite the name similarity, they are completely different).
lansingstudent09101
2010-12-02 23:04:30 UTC
I don't know about that specific script, but running your javascript on someone elses page is simple:



take for instance a short script like

alert("Hello World");



to run this on any page, put it in your address bar like this:



javascript:alert("Hello World");void(0);



The void(0) is important because it stops redirecting to a page with the return value on it. IE it stops you from leaving the page. (alert boxes won't actually make you leave the page, but many, many results will navigate you way from your current page to a "page" with just your result on it)



As for interacting with the script, I'd recommend getting firebug and using the javascript debugger to find out what the script is doing. http://getfirebug.com
2016-10-19 08:49:19 UTC
In laymens words, JavaScript is a working laptop or laptop language that provides greater administration to a internet website than is granted with universal html, it extremely is the language internet pages are written in.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...