Rectangle 27 95

For reading the external Local JSON file (data.json) using javascript, first create your data.json file:

data = '[{"name" : "Ashwin", "age" : "20"},{"name" : "Abhinandan", "age" : "20"}]';

Mention the path of the json file in the script source along with the javascript file.

<script type="text/javascript" src="data.json"></script>
<script type="text/javascript" src="javascrip.js"></script>
  • Get the Object from the json file var mydata = JSON.parse(data); alert(mydata[0].name); alert(mydata[0].age); alert(mydata[1].name); alert(mydata[1].age);

This works if you can modify the file, or if the file does not have proper JSON as its contents. For instance the sample content for data.json above does not pass validation: jsonlint.com because it is really JavaScript. Dropping the wrapping single quotes would turn it into pure JavaScript.

JSON.parse
window.JSON

Shouldn't JSON.parse(data); not work because data is a string?

This is not a correct answer. The example in the answer is not loading a json file. It is actually just loading another javascript file which stores some hardcoded json as a variable named data. If you removed the string quotes from around the json in data.json you wouldn't even need to use JSON.parse. The question is how to load a JSON file not how to hardcode JSON into another javascript file and then load it.

JSON.parse(window.data); would provide better information of the scope of the data variable.

How to read an external local JSON file in Javascript - Stack Overflow

javascript json
Rectangle 27 95

For reading the external Local JSON file (data.json) using javascript, first create your data.json file:

data = '[{"name" : "Ashwin", "age" : "20"},{"name" : "Abhinandan", "age" : "20"}]';

Mention the path of the json file in the script source along with the javascript file.

<script type="text/javascript" src="data.json"></script>
<script type="text/javascript" src="javascrip.js"></script>
  • Get the Object from the json file var mydata = JSON.parse(data); alert(mydata[0].name); alert(mydata[0].age); alert(mydata[1].name); alert(mydata[1].age);

This works if you can modify the file, or if the file does not have proper JSON as its contents. For instance the sample content for data.json above does not pass validation: jsonlint.com because it is really JavaScript. Dropping the wrapping single quotes would turn it into pure JavaScript.

JSON.parse
window.JSON

Shouldn't JSON.parse(data); not work because data is a string?

This is not a correct answer. The example in the answer is not loading a json file. It is actually just loading another javascript file which stores some hardcoded json as a variable named data. If you removed the string quotes from around the json in data.json you wouldn't even need to use JSON.parse. The question is how to load a JSON file not how to hardcode JSON into another javascript file and then load it.

JSON.parse(window.data); would provide better information of the scope of the data variable.

How to read an external local JSON file in Javascript - Stack Overflow

javascript json
Rectangle 27 95

For reading the external Local JSON file (data.json) using javascript, first create your data.json file:

data = '[{"name" : "Ashwin", "age" : "20"},{"name" : "Abhinandan", "age" : "20"}]';

Mention the path of the json file in the script source along with the javascript file.

<script type="text/javascript" src="data.json"></script>
<script type="text/javascript" src="javascrip.js"></script>
  • Get the Object from the json file var mydata = JSON.parse(data); alert(mydata[0].name); alert(mydata[0].age); alert(mydata[1].name); alert(mydata[1].age);

This works if you can modify the file, or if the file does not have proper JSON as its contents. For instance the sample content for data.json above does not pass validation: jsonlint.com because it is really JavaScript. Dropping the wrapping single quotes would turn it into pure JavaScript.

JSON.parse
window.JSON

Shouldn't JSON.parse(data); not work because data is a string?

This is not a correct answer. The example in the answer is not loading a json file. It is actually just loading another javascript file which stores some hardcoded json as a variable named data. If you removed the string quotes from around the json in data.json you wouldn't even need to use JSON.parse. The question is how to load a JSON file not how to hardcode JSON into another javascript file and then load it.

JSON.parse(window.data); would provide better information of the scope of the data variable.

How to read an external local JSON file in Javascript - Stack Overflow

javascript json
Rectangle 27 4

The GapSourceDictionary.xml XML file lists all the HTML application resources. When the application starts, this XML file is read and all the files included in the list are added to the isolated storage so that it can be served by the WebBrowser control.

<?xml version="1.0" encoding="utf-8"?>
    <GapSourceDictionary>
        <FilePath Value="www\index.html"/>
        <FilePath Value="www\test.css"/>
        <FilePath Value="www\phonegap-1.3.0.js"/>
        <FilePath Value="www\js\custom.js"/>
    </GapSourceDictionary>

In your HTML files use relative path to specify the URL of the external script file:

<script type="text/javascript" src="./js/custom.js"></script>

I managed with success to dynamically load a new local JavaScript file using the following procedure. The test was successful on PhoneGap 2.0. Unfortunately the same test failed on PhoneGap 1.8.1

Below is the source code which loads the JS dynamically:

function dynamicJSload(url)
{
    var script = document.createElement('script');
    script.type = "text/javascript";
    if (script.readyState)
    {
        script.onreadystatechange = function(){
            if (script.readyState == "complete" || script.readyState == "loaded"){
                script.onreadystatechange = null;
            }
        };
    }
    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}
dynamicJSload("js\\test.js");

thanks for your answer... I already have those files in GapSourceDictionary.xml file and the relative path unfortunatelly gives me an Access Denied error as well

Could you provide some information about the folders structure? You should have a file www inside the main project's folder and this file should contain all the HTML, JavaScript, CSS and images.

If the dynamic HTML content loading is performed through JQ AJAX, please ensure that you have set the dataType to html (dataType: "html"). IE mobile expects exactly the dataType you have set and will not receive the content in case of a wrong dataType.

I successfully managed to load a new JS in a demo page. The test was performed using the Windows Phone 7 Emulator's IE browser. I'll update my post

jquery mobile - Include local JavaScript within PhoneGap on Windows Ph...

windows-phone-7 jquery-mobile cordova
Rectangle 27 2

Nope. There's no Javascript API for loading the true content of <script> tags. This is actually not an oversight, but rather a security feature: suppose I request the .json file that Gmail requests via AJAX to load your inbox by putting it in an external <script> tag. A JSON document is valid Javascript (granted, without side-effects), so it would run without error. Then, if I could inspect the content of the external script, I would be able to read your e-mail. (I'm almost certain that Gmail is more complex than that, but most sites are not.)

So, making up a few things about how Gmail works, here's how the attack would look:

<script id="inbox" type="text/javascript" src="http://mail.google.com/OMGYOURINBOX.json"></script>

<script type="text/javascript">
// Supposing a value called `externalScriptContent` existed on a script tag:
var inboxJSON = document.getElementById('inbox').externalScriptContent;
var messages = JSON.parse(inboxJSON);
for(var i in messages) {
  // Do something malicious with each e-mail message
  alert(messages[i].body);
}
</script>

If a script tag had the value externalScriptContent, I could just put whatever URL in for the src that I wanted, and then summon up the remote file's contents, effectively circumventing AJAX cross-origin restrictions. That'd be bad. We allow cross-origin requests for remote scripts because they are run and run only. They cannot be read.

Firebug has these permissions because Firefox extensions have the ability to inspect anything that the browser requests; normal pages, thankfully, do not.

However! Bear in mind that, if the script is on your domain, instead of writing it in <script src=""></script> form, you can pull it up with an AJAX request then eval it to have access to the contents and still only request it once :)

If you can request the .json script that Gmail publishes then you have read my email regardless of whether the source is available for inspection, is that not right? Authentication is the mechanism that prevents this situation. Likely I'm not understanding the theoretical situation proposed.

@John K: AJAX requests are subject to cross-origin permission requirements, but calls to external script files via <script src=""></script> are not. If I were able to inspect the content of an external script summoned via <script src=""></script>, then I could just put whatever I wanted as the src, read it, and effectively circumvent those cross-origin restrictions. The reason that <script src=""></script> is not subject to those requirements is that the file runs and only runs; it cannot be read. I just caught my misleading phrasing in my answer, though, so edited :)

dom - Is external JavaScript source available to scripting context ins...

javascript dom
Rectangle 27 0

And you must made fs.readFile wrapped by a closure, otherwise some file (especially the last file) will be read more than once, and others will not be read at all. And the contentTypewill not be set as you wish. This is because of the callback strategy used by fs.readFile. The problem does not appear when the html file just load one external file, but as the external files(css, js, png) loaded more than one it will appear as i pointed out above. (I came upoon this by myself)

So your code should make a little change as follows:

;(function (filename, contentType) {

    fs.readFile(filename, function(err, file) {
        // do the left stuff here
    });

}(filename, contentType));

socket.io and differents folders --- solution found - Stack Overflow

socket.io
Rectangle 27 0

And you must made fs.readFile wrapped by a closure, otherwise some file (especially the last file) will be read more than once, and others will not be read at all. And the contentTypewill not be set as you wish. This is because of the callback strategy used by fs.readFile. The problem does not appear when the html file just load one external file, but as the external files(css, js, png) loaded more than one it will appear as i pointed out above. (I came upoon this by myself)

So your code should make a little change as follows:

;(function (filename, contentType) {

    fs.readFile(filename, function(err, file) {
        // do the left stuff here
    });

}(filename, contentType));

socket.io and differents folders --- solution found - Stack Overflow

socket.io