调用en外部javascript文件的功能

时间:2020-11-27 16:00:31

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

一般来说......如何在外部java脚本文件的函数上调用?

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.

    外部javascript文件(我想调用)FontSize.js包含以下函数。

    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

    FontSize.js位于〜/ Jscript /目录中

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

当然没有任何工作,因为,我不知道如何调用函数到外部js文件

2 个解决方案

#1


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.

当您声明一个函数并且它不在另一个对象/命名空间中时,它只是全局可用,您可以将其称为它紧接在当前代码之前。默认情况下,这些函数将在window对象上,您可以在这里看到一个简短的演示。

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

#2


1  

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

#1


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.

当您声明一个函数并且它不在另一个对象/命名空间中时,它只是全局可用,您可以将其称为它紧接在当前代码之前。默认情况下,这些函数将在window对象上,您可以在这里看到一个简短的演示。

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

#2


1  

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