最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is a global variable undefined inside a function when I call it? - Stack Overflow

programmeradmin0浏览0评论

This function redirects to a URL, but the course_syllabus_code part shows up as undefined after the URL. I don’t know why this happens.

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.

const course_syllabus_code = "72524";

function syllabusLink(course_syllabus_code){
  location.href = "/" + course_syllabus_code;
}
<ul>
  <li>
    <a href="javascript:syllabusLink();">Syllabus</a>
  </li>
</ul>

This function redirects to a URL, but the course_syllabus_code part shows up as undefined after the URL. I don’t know why this happens.

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.

const course_syllabus_code = "72524";

function syllabusLink(course_syllabus_code){
  location.href = "https://example./" + course_syllabus_code;
}
<ul>
  <li>
    <a href="javascript:syllabusLink();">Syllabus</a>
  </li>
</ul>

Share Improve this question edited Nov 1, 2022 at 12:02 Sebastian Simon 19.5k8 gold badges61 silver badges84 bronze badges asked Mar 9, 2013 at 10:33 rhslogicrhslogic 3592 gold badges5 silver badges10 bronze badges 3
  • 2 You never pass in course_syllabus_code so it shows up as undefined because it is not present. – Travis J Commented Mar 9, 2013 at 10:35
  • Please note that a construct like href="javascript:syllabusLink();" should never be used. See Why is it bad practice to use links with the javascript: "protocol"?. Instead, a proper, accessible fallback URL should be used in the href attribute while the syllabusLink function should be bound as an event listener via addEventListener. Since all the answers below reference this piece of code, it’s a bit of a hurdle to edit it out for the benefit of future readers. – Sebastian Simon Commented Nov 1, 2022 at 12:08
  • Related: Global variable is logged as undefined when passed as parameter to setTimeout callback function. – Sebastian Simon Commented Dec 5, 2022 at 8:32
Add a ment  | 

6 Answers 6

Reset to default 7

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 e 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.'+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.' + 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.'+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./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./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.'+course_syllabus_code;
}
</script>

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

发布评论

评论列表(0)

  1. 暂无评论