简体   繁体   中英

Function producing undefined variable

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.

<script type="text/javascript">

var course_syllabus_code = "72524";

function syllabusLink(course_syllabus_code) {
location.href = 'https://foo.com'+course_syllabus_code;
}
</script>

<ul>
<li><a href="javascript:syllabusLink()"><img src="CourseButton3.png" />Syllabus</a></li>
</ul>

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):

  1. Use the global by just removing the declared function argument:

     function syllabusLink() { // Removed here ------^ 
  2. Or , pass the global into the function where you call it:

     <a href="javascript:syllabusLink(course_syllabus_code)"><img src="CourseButton3.png" />Syllabus</a> 

    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; } 
  3. Or , don't make course_syllabus_code a global at all, and pass it to the function:

     <a href="javascript:syllabusLink('72524')"><img src="CourseButton3.png" />Syllabus</a> 

I'd probably go for #3, unless there's a really good reason it has to be a global variable.

Change the function argument since it's the same name as the local variable.

var course_syllabus_code = "72524";

function syllabusLink(code) {
    location.href = 'https://foo.com' + code;
}

You should pass course_syllabus_code to syllabusLink within the href attribute.

<a href="javascript:syllabusLink(course_syllabus_code)"><img src="CourseButton3.png" />Syllabus</a>

Try like this

  var course_syllabus_code = "72524";

function syllabusLink() {
location.href = 'https://foo.com'+course_syllabus_code;
}

Because in onClick() you are not passing any parameter.

course_syllabus_code is global variable.You can access directly in the function.

This bit is incorrect:

javascript:syllabusLink()

You are missing the parameter so calling the function

function syllabusLink(course_syllabus_code) {

The value of course_syllabus_code is not being set.

Please never use the javascript: protocol except for bookmarklets.

The better code is this

<script type="text/javascript">

function syllabusLink(course_syllabus_code) {
   location.href = 'https://foo.com/course?syllabus='+course_syllabus_code;
   return false; // cancel link
}
</script>

<ul>
<li><a href="#" onclick="return syllabusLink('72524')"><img src="CourseButton3.png" />Syllabus</a></li>
</ul>

Or

this

<script type="text/javascript">
var course_syllabus_code="72524";

function syllabusLink() {
   location.href = 'https://foo.com/course?syllabus='+course_syllabus_code;
   return false; // cancel link
}
</script>

<ul>
<li><a href="#" onclick="return syllabusLink()"><img src="CourseButton3.png" />Syllabus</a></li>
</ul>

Simple answer you need to pass course_syllabus_code as argument in function . just remove it from function argument and try.

<script type="text/javascript">
var course_syllabus_code = "72524";
function syllabusLink() {
    location.href = 'https://foo.com'+course_syllabus_code;
}
</script>

And the reason is course_syllabus_code is global variable .You can access directly in the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM