Php writes a web project to write a web project. Is it better to put JavaScript in a single js file, or to directly write it in an html page? What are the advantages and disadvantages?
Reply to discussion (solution)
It must be taken out separately. If an error occurs, it is easy to find it.
Easy to develop and can be reused on multiple pages
In combination, the number of connections on the server is less than 1, but this has no impact on small websites. large websites can also use cache to eliminate this impact.
Therefore, it is better to separate
It is better to separate JS files
It is better to use the tp framework for separate JS files. if ajax requests exist, the project name must be included in the url. But if I change the project name, wouldn't I change the url of each js? Is there any simple method? If I write it on an html page, I can directly use constants defined by the tp framework, such as/index. php.
It must be taken out separately. If an error occurs, it is easy to find that I use the tp framework. if there is an ajax request, there must be a project name in the url. But if I change the project name, wouldn't I change the url of each js? Is there any simple method? If I write it on an html page, I can directly use constants defined by the tp framework, such as/index. php.
Easy to develop and can be reused on multiple pages
In combination, the number of connections on the server is less than 1, but this has no impact on small websites. large websites can also use cache to eliminate this impact.
Therefore, it is better to use the tp framework separately. if ajax requests exist, the project name must be included in the url. But if I change the project name, wouldn't I change the url of each js? Is there any simple method? If I write it on an html page, I can directly use constants defined by the tp framework, such as/index. php.
Take it out to facilitate modification! It is also easy to call other files!
It must be separated to facilitate code reuse and clear structure!
If you're so big, you can do it yourself.
It's like you have made a lot of friends and you have some frequent contacts. do you want to put them in a group of mobile phones.
Some of them may only contact once or twice, so they don't need to be placed in the group, right?
If you're so big, you can do it yourself.
It's like you have made a lot of friends and you have some frequent contacts. do you want to put them in a group of mobile phones.
Some of them may only contact once or twice, so they don't need to be placed in the group, right? Dizzy, I am now considering the path issue ..
Dizzy, I am now considering the path issue ..
Js files usually use the absolute path of the website
If offline debugging is required, use the relative path and make sure to reference it downward.
That is, the js file is in the local or lower-level Directory of the referenced file.
It is better to separate ....
Reference the reply from wanql0203 on the 10th floor: dizzy, I am considering the path problem ..
Js files usually use the absolute path of the website
If offline debugging is required, use the relative path and make sure to reference it downward.
That is, the js file uses the tp framework in the reference file's local or lower-level Directory. if there is an ajax request, there must be a project name in the url. But if I change the project name, wouldn't I change the url of each js? Is there any simple method? If I write it on an html page, I can directly use constants defined by the tp framework, such as/index. php.
Does the tp framework have special ajax support?
You just press him.
Does the tp framework have special ajax support?
You just want to say that I use jquery ,,,
I don't know what you're asking.
If JavaScript is used on many pages, it is better to put it in the js file. if it is used only on a page, you can directly write it to the page.
The public is written in the js file, and the use of a single page is written on the current page.
I don't know what you are asking. I wrote ajax using jquery. The Path uses an absolute path, that is, the project name must be included. If the project name is modified in the future, isn't every ajax path required to be modified? I just want to ask if there is a way to simplify it without changing every path.
If I put js in html, I don't need to consider the path. Instead, I can use the tp constant/index. php instead, without the project name involved. If you modify the project name, you do not need to modify the ajax path name.
So I asked this question ..
In any case, requirements always come first!
Now that you have used the path in js, you do not want to change it to a relative path.
So is it unnecessary to discuss built-in or external connection?
Of course, you can also embed an independent js file into the main template as a sub-template.
You can create a js file that stores specific paths. This File defines all path variables. Other js files use the path variables defined in this file. Then the js file that stores the path can be introduced before other js files.
For example, define path. js
It defines var filePath = "****";
The filePath is used as the path in other files.
When used, define in the page: