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

asp.net - Calling a function of en external javascript file - Stack Overflow

programmeradmin2浏览0评论

In general... How can I make a call on a function of an external java script file?

More specific...

  • In the head tag i have

<script type="text/javascript" src="JScript/FontSize.js"></script>

  • The external javascript file, (that i would like to call) FontSize.js contains the following functions.

    function checkCookie()
    
    function setCookie(c_name, value, expiredays)
    
    function getCookie(c_name)
    
    function increaseFontSize()
    
    function decreaseFontSize()`
    
  • The FontSize.js is located at the ~/Jscript/ directory

I guess the body on load should contain something like

<body onload="/JScript/Fontsize.js/checkCookie()">

Of course nothing works as it should because, i do not know how to make the call to a function to an external js file

In general... How can I make a call on a function of an external java script file?

More specific...

  • In the head tag i have

<script type="text/javascript" src="JScript/FontSize.js"></script>

  • The external javascript file, (that i would like to call) FontSize.js contains the following functions.

    function checkCookie()
    
    function setCookie(c_name, value, expiredays)
    
    function getCookie(c_name)
    
    function increaseFontSize()
    
    function decreaseFontSize()`
    
  • The FontSize.js is located at the ~/Jscript/ directory

I guess the body on load should contain something like

<body onload="/JScript/Fontsize.js/checkCookie()">

Of course nothing works as it should because, i do not know how to make the call to a function to an external js file

Share Improve this question edited Jul 3, 2014 at 6:33 Arun Chandran Chackachattil 3,3627 gold badges33 silver badges45 bronze badges asked May 29, 2010 at 12:37 OrElseOrElse 9,95940 gold badges148 silver badges263 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 13

You just call it as if it were local :)

<body onload="checkCookie()">

Or, do it in script:

window.onload = checkCookie;

When you declare a function and it's not in another object/namespace, it's just globally available, and you can call it as if it immediately preceded your current code. By default these functions will be on the window object, you can see a short demo here.

For example (doesn't matter where this function's defined, external or not):

function myFunc() { alert('hi'); }
myFunc();
window.myFunc(); //same call, unless there's *another* myFunc in a local-er scope
  <html>
        <head>
            <script type="text/javascript" language="javascript" src="main.js"></script>
        </head>
        <body>

    <!--The extranal main.js file contains samp() function.. -->
            <script>
              <!--    samp(); -->
            </script>
        </body>
    </html>
发布评论

评论列表(0)

  1. 暂无评论