You're passing course_syllabus_code
as an argument to the function, but calling the function without any arguments. When you call a function with fewer arguments than it defines, the ones that you don't pass come through with the value undefined
. Since you've given the function argument the same name as the global, it "shadows" (obscures, hides) the global from the code in the function, which will access the argument instead.
You have three choices (only do one of these, not all of them):
-
Use the global by just removing the declared function argument:
function syllabusLink() { // Removed here ------^
-
Or, pass the global into the function where you call it:
href="javascript:syllabusLink(course_syllabus_code)">
src="CourseButton3.png" />Syllabus
I'd probably change the name of the argument as well (here I've used
scode
):function syllabusLink(scode) { location.href = 'https://foo.com'+scode; }
-
Or, don't make
course_syllabus_code
a global at all, and pass it to the function:href="javascript:syllabusLink('72524')">
src="CourseButton3.png" />Syllabus
I'd probably go for #3, unless there's a really good reason it has to be a global variable.
manpreet
Best Answer
2 years ago
I am working within a Learning Management System and creating some custom components
This is returning the url but the "course_syllabus_code" shows up as undefined after the url. I am a beginner to Javascript and have looked at some of the other answers on Google but still cannot figure it out.
Basically the user will be able to open the code and enter the Variables(There will be multiple) at the top. Then further below is where all the magic is supposed to happen...
The structure of the variable being entered cannot change but maybe the "magic" isn't being executed in the best way possible.