2

I have a long markdown file. I also have a javascript file that runs a parser over markdown. In my javascript file I have set:

var text = "md/markdown.md"

This does not seem to pull in the content of the markdown file as I would like it to. However, if I copy and paste the contents of the markdown file into the variable, then everything works as it should. Is there a way I can set this javascript variable to pick up the contents of this external markdown file?

11
  • Are you running a standalone Javascript interpreter, or are you running this in a web browser? Commented Dec 29, 2012 at 0:30
  • 1
    Under no circumstances does an assignment to a JavaScript variable trigger an HTTP request to load anything. (OK it would if you bound a setter to an ajax routine, but I don't think that's what we're talking about here :-) Commented Dec 29, 2012 at 0:30
  • He obviously does not understand that. I don't see how it's helpful to point it out, or how upvoting it does anything for anyone Commented Dec 29, 2012 at 0:30
  • 3
    @Pointy — location = 'http://example.com/' :) Commented Dec 29, 2012 at 0:31
  • 2
    @uʍopǝpısdn it's a general clarification that the semantics of the language are fundamentally different than what's been assumed. The whole point of Stackoverflow is to have other people tell you what you've done wrong :-) Commented Dec 29, 2012 at 0:31

3 Answers 3

1

In the context of a web browser, if you want JavaScript to fetch data from a URI then you will generally use the XMLHttpRequest object. MDN has a decent tutorial about using XMLHttpRequest.

Most of the general purpose JavaScript libraries include wrappers for XHR that include compatibility fixes (especially for old-IE). I'm fond of YUI. Another option is the relatively ubiquitous jQuery.

It isn't a problem for the case given in the question, but beware of the same origin policy.

Sign up to request clarification or add additional context in comments.

Comments

1

Run an AJAX request:

var ajaxRequest, text;  

try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
} catch (e){
    // Internet Explorer Browsers
    try{
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
}
ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
        text = ajaxRequest.responseText;
    }
}

1 Comment

You've failed to check that the response had an OK status, set a URL to make the request to and to send the request. The comments are somewhat dated too, you mention some specific browsers, but not Chrome, and you don't mention that recent versions of IE have support for the standard XHR object.
1

Javascript cannot directly read local files for security reason. As an alternative way, you can take use of XMLHttpRequest to achieve it. Please check the link on the stackoverflow: read external file with Javascript. In addition, HTML5 provides a standard way to interact with local files, via the File API specification. You may refer to the tutorial: http://www.html5rocks.com/en/tutorials/file/filesystem/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.