Share icon
If we need to use the php variable in our js file we can use Core Drupal.settings Functionality :

 example:

Let we assume we want to use at the page load so, add this is hook_init():

function mymodule_init(){

$token_value = "this is value of your variable";

drupal_add_js(array('mymodule' => array('token' => $token_value)), array('type' => 'setting'));

}

Now get the in your js file, but make sure your js file also preloaded to website via theme.info or any other way ...

jQuery(document).ready(function(){

alert(Drupal.settings.mymodule.token);

});

That it...

Comments

Profile picture for user Duan Camargo
genawellish6412 (not verified)
Mon, 08/02/2021 - 17:34
Profile picture for user Duan Camargo
geniedorron8118 (not verified)
Mon, 08/02/2021 - 22:17
Profile picture for user Duan Camargo
kassandraspear… (not verified)
Tue, 08/03/2021 - 18:13
Profile picture for user Duan Camargo
jamilagrimwade379 (not verified)
Wed, 08/04/2021 - 18:41
Profile picture for user Duan Camargo
leticiahalligan689 (not verified)
Fri, 08/06/2021 - 19:30
Profile picture for user Duan Camargo
andyp313534018… (not verified)
Sat, 08/07/2021 - 14:21

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.