Question:
Javascript: returning variables from external sources?
Brandon
2008-02-10 12:47:55 UTC
I'm trying to make a widget and I have a decent understanding of HTML, JS, and DOM. I need to be able to retrieve a variable from *another* website without opening the page inside of the browser. Opening the page in the widget and keeping it hidden would work, too. Is there a command for JS to load the contents of page and make it accessible to define variables, perform functions on, etc?
Three answers:
richarduie
2008-02-10 14:21:36 UTC
The problem isn't retrieving the external document - that's pretty trivial. The content can even be hidden, using display 'none' for #doc style in the sample. The issue that stops you is the browser security model with respect to cross-site scripting and domain-origin policies. You aren't allowed to use JavaScript from a page originating in one domain to access DOM elements of a document that originates in another domain, even if the other document is embedded within a child element of your own page.



In the sample, the first button just alternately loads Google and Yahoo! front pages to the iframe. When the Yahoo! page is in the iframe, the second button will try to retrieve the element with id "searchlabel" (just the string "Search:"). If you check the JavaScript console, you'll see that this is a permission violation.






















value ="first "

onclick="getDoc();"/>


value ="second"

onclick="getHTML();"/>











Spearfish
2008-02-10 13:02:44 UTC
Sorry, no.



The problem with JavaScript is that the variables it uses are local variables... they're only good within the script. JS cannot import anything from or export anything to external sources.



Perhaps you need PHP and / or MySQL?
?
2016-10-18 06:31:13 UTC
u could additionally purely say "upload.Hypertext Preprocessor" cus whether that's interior an identical itemizing and the document ur attempting to get entry to, then u can purely sya thy call, and it will look interior the present itemizing. if u desire to look in a deeper directyory, u can say "/testdir/upload.Hypertext Preprocessor"


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