1

I want to load the fragment shader from an external file, but it dont works. (after the shader is loaded, the alertbox don't appear.)

var fs = document.createElement('script');
fs.setAttribute("type","x-shader/x-fragment");
fs.setAttribute("src", "shader.fs");

fs.onload = function() {alert('done');}
document.getElementsByTagName("head")[0].appendChild(fs);
4
  • Not all browsers trigger a "load" event on <script> elements. Commented Dec 12, 2011 at 14:15
  • Better use XmlHttpRequest, more lean and faster. Commented Dec 12, 2011 at 14:15
  • ok, but how can I load shaders with XmlHttpRequest? Commented Dec 12, 2011 at 14:17
  • Are you using the learningwebgl.com tutorials? Commented Dec 12, 2011 at 20:19

1 Answer 1

4

Storing a shader in a script tag is simply a convention that several WebGL tutorials picked up to give the shader a simple place to "live". It doesn't have to be put in a script tag to work.

XHR is probably the easiest way to pull one down.

var shaderXhr = new XMLHttpRequest();
shaderXhr.open("GET", "shader.fs", true);
shaderXhr.onload = function() {
    yourShaderParsingRoutine(this.responseText);
};
shaderXhr.send(null);
Sign up to request clarification or add additional context in comments.

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.