how may i use function in external java script file in html pages | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

how may i use function in external java script file in html pages

20th Jul 2017, 10:06 AM
Saeed Noruzi
Saeed Noruzi - avatar
9 ответов
0
Lowercase c in onclick, not capital C. I know, it doesn't follow the camelCase convention. Also, include the brackets when you call a function. onclick="click()"
20th Jul 2017, 12:00 PM
James
James - avatar
0
In your HTML, include the following code. <script src="script.js"></script> It works best when put at the very end of your <body>, just before you close it. Replace "script.js" with the pathname of your file (search pathname HTML src). If it is in the same folder, you can write it simply as I have done without the need to specify the path. Now in the JavaScript file you can reference elements in your HTML (search DOM JavaScript). You use onclick="funct();" as an attribute to call a function from the JS file. Also, if you link another JS file to your HTML, you can use the same functions and variables across the different codes (to avoid this place all your code within a function).
20th Jul 2017, 11:06 AM
James
James - avatar
0
i did what you told but it just doesn't work
20th Jul 2017, 11:34 AM
Saeed Noruzi
Saeed Noruzi - avatar
0
How are your files saved? How are you referencing them for src=" "? Could the problem be with your JS code not performing the action? Just write this on the first line of it and you'll know if the linking works. document.body.innerHTML = "It works"; OR alert("It works");
20th Jul 2017, 11:39 AM
James
James - avatar
0
well i have html file named index.html and java script file named script.js both file are in same folder i used script tag in the end of body tag i wrote my function in script file now i want call that function on my html file i used on click event to call my function but it doesn'twork @ all i watch the source view in browser the script file is bound to html file but i dont know why it doesn'twork
20th Jul 2017, 11:45 AM
Saeed Noruzi
Saeed Noruzi - avatar
0
Sounds like you have linked it properly. The problem might be your JS or the way you have used onclick. See if the test I posted above works.
20th Jul 2017, 11:48 AM
James
James - avatar
20th Jul 2017, 11:59 AM
Saeed Noruzi
Saeed Noruzi - avatar
0
ah i am gonna get crazy
20th Jul 2017, 12:03 PM
Saeed Noruzi
Saeed Noruzi - avatar
0
horay i found the real problem i should put these symbols ( ) after function name in event
20th Jul 2017, 2:50 PM
Saeed Noruzi
Saeed Noruzi - avatar