What are the advantages to putting your Javascript in a .php file?

前端 未结 7 2454
独厮守ぢ
独厮守ぢ 2021-02-15 12:40

I occasionally come across pages where some Javascript is included via a PHP file:


  
    

        
7条回答
  •  旧巷少年郎
    2021-02-15 13:25

    Absolutely none, IMHO. I use a js framework that I wrote to handle the setting of whatever server-side variables I need to have access to. It is essentially the same as embedding PHP in JavaScript, but much less ambiguous. Using this method allows you to also completely separate server-side logic and html away from javascript. This results in much cleaner, more organized and lowly-coupled modular code.

    You could do something like this in your html:

    
    

    And then do something like this in your js:

    if (registry.myInt === 1) {
        alert(registry.myString);
    }
    

提交回复
热议问题