files/en-us/web/api/webglrenderingcontext/attachshader/index.md
{{APIRef("WebGL")}}{{AvailableInWorkers}}
The WebGLRenderingContext.attachShader() method of the WebGL API attaches either a fragment or vertex {{domxref("WebGLShader")}} to a {{domxref("WebGLProgram")}}.
attachShader(program, shader)
program
shader
None ({{jsxref("undefined")}}).
The following code attaches pre-existing shaders to a {{domxref("WebGLProgram")}}.
const program = gl.createProgram();
// Attach pre-existing shaders
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
const info = gl.getProgramInfoLog(program);
throw new Error(`Could not compile WebGL program. \n\n${info}`);
}
{{Specifications}}
{{Compat}}