I have a variable/flag in a PHP file and depending upon the value of this flag i need to execute a set of code in another .js file. Can you tell me how to access the variable in .php file inside a .js file? Please find attached the sample js file i am using. I need to execute the set of code inside this file depending on the value of the flag.

I would suggest changing 'sample.js' to 'sample.php' then use sessions to transfer the value and echo the value into the file. A sample is as below and make sure the first line is exactly the same:

<? session_start(); ?>
//to display the value in the javascript
var variable=<? echo $_SESSION['variable']; ?>;
						case 'ol' :
						case 'ul' :
							var isFirstLevel = !htmlNode.parentNode.nodeName.IEquals( 'ul', 'ol', 'li', 'dl', 'dt', 'dd' ) ;

							this._AppendChildNodes( htmlNode, stringBuilder, prefix ) ;

							if ( isFirstLevel && stringBuilder[ stringBuilder.length - 1 ] != "\n" ) {
								stringBuilder.push( '\n' ) ;
							}

							break ;

						case 'li' :

							if( stringBuilder.length > 1)
							{
								var sLastStr = stringBuilder[ stringBuilder.length - 1 ] ;
								if ( sLastStr != ";" && sLastStr != ":" && sLastStr != "#" && sLastStr != "*")
									stringBuilder.push( '\n' + prefix ) ;
							}

							var parent = htmlNode.parentNode ;
							var listType = "#" ;

							while ( parent )
							{
								if ( parent.nodeName.toLowerCase() == 'ul' )
								{
									listType = "*" ;
									break ;
								}
								else if ( parent.nodeName.toLowerCase() == 'ol' )
								{
									listType = "#" ;
									break ;
								}
								else if ( parent.nodeName.toLowerCase() != 'li' )
									break ;

								parent = parent.parentNode ;
							}

							stringBuilder.push( listType ) ;
							this._AppendChildNodes( htmlNode, stringBuilder, prefix + listType ) ;

							break ;

And in the php file use the following with the first session_start() being in the first line or in the php headers:

<? session_start();
//your headers
//your code
$_SESSION['variable']=$myvariable; //must be before including javascript file.
//include javascript file
//your code
?>

Note that the Javascript file with the php code (first php box) must execute after the session variable has been set. So follow the design of the above code box for file execution and you should be fine on that one. Also note that the javascript file needs that php extension.

thanks for the reply.. but i want to do it the other way as these files are part of a bigger appln and all these files and codes already exist...changing the .js file into a php might affect other functionalities..... i have to set a variable in a settings.php file and then depending on the value of that variable execute the code in the atttached js file. the attached code is just a part of a bigger code base.....

how can we create new scripts with the help of php

I have used javascript to detect the screen size then refreshed the page with for example postme.html?javaform=script2

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.