Skip to main content
Key2Goal

Main navigation

  • Home
  • About Us
  • Contact us
  • Glossary
  • Privacy Policy
User account menu
  • Log in

Breadcrumb

  1. Home

send Variable to Js File From PHP in drupal Custom Module

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...

  • Log in to post comments

Comments

Article category

  • Drupal
  • Wordpress
  • Java
  • Javascipt
  • HTML
  • CSS
  • IOS
  • Android
  • PHP
  • MySQL
  • Other
  • Command line
  • NodeJs
  • Drupal 8
  • Drupal 7
  • GIT
  • DevOps
  • Python
  • Xampp
  • Server
  • Artificial intelligence
  • AppScript

Recent content

  • Differences Between Bootstrap 3 & Bootstrap 4
    5 hours 23 minutes ago
  • About Us
    10 hours 17 minutes ago
  • Privacy Policy
    12 hours 10 minutes ago
  • Create a Virtual Host on Xampp in 5 steps
    1 month 1 week ago
  • How to setup Multisite in drupal 9
    7 months 3 weeks ago
  • How to creating a FOR loop and a WHILE loop in JavaScript
    7 months 3 weeks ago
  • Calculator program HTML & PHP
    7 months 3 weeks ago
  • Algothrim to create a web application
    7 months 3 weeks ago
  • 10 Git Useful Commands for Every Developer
    7 months 3 weeks ago
  • Top 10 useful commands for GIT with example
    7 months 3 weeks ago
© Copyright 2017 - 2026 key2Goal. All Rights Reserved