Using PHP variables in Javascript include files for view in MVC -


Let's say I'm using an MVC framework, and my thoughts should be located in the external file to see the javascript files.

My javascript based in the external file depends, however, on some PHP variables in my view. If I want JavaScript to be included in the tag inside my HTML view, then I can just inject the PHP variable into javascript.

I know that I can create hidden input fields and allocate the variable as their values. . Is there any other way more beautiful?

I know that I can take the .php file with a .php name, but I'm not so much interested to do this.

I'm not sure that I fully understand your question, but if I have the right , Then you can not just set the global JavaScript variable inside a script tag in the view, which will be passed to your external JS?

  & lt; Script type = "text / javascript" & gt; Var myvar1 = & lt ;? = $ Myvar1? & Gt ;; Var myvar2 = "& lt;? = $ Myvar2? & Gt;"; & Lt; / Script & gt; & Lt; Script type = "text / javascript" src = "myexternaljs.js" & gt; & Lt; / Script & gt;  

Comments

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog.
    Really very informative post you shared here. Kindly keep blogging.
    If anyone wants to become a Front end developer learn from Javascript Online Training from India .
    or learn thru JavaScript Online Training from India.
    Nowadays JavaScript has tons of job opportunities on various vertical industry. ES6 Training in Chennai

    ReplyDelete

Post a Comment