I've been developing my site with the site directory structure as
www
img
javascript
include
index.php
Now I want to change it to
www
index.php
dir1
dir2
...
themes
theme1
img
javascript
include
index.php
At the theme level index.php, earlier I've my paths to javscript files as javascript/file1.js or so.
Now I've changed the paths to <?php echo THEME_PATH . "javascript/file1.js"?>
This includes the js files, however my problem is when I reached inside the js file.
For example, in one script.js file, I've a jquery method called as
$('#pop-con').html('<p><img src="img/ajax-loader.gif" width="220" height="19" /></p>');
I can't use php inside the js file. So how do I change the paths similar to the php implementation above??
Relative paths
Probably one of the easiest ways to solve it is using relative paths.
In your case you will need to be one directory up (../), so it would be:
$('#pop-con').html('<p><img src="../img/ajax-loader.gif" width="220" height="19" /></p>');
You can have a small <script> on your pages that creates a global variable to store the "THEME_PATH" value:
<script>
window['THEME_PATH'] = '<?php echo THEME_PATH?>';
</script>
Then your JavaScript files can just look for the global variable and use it to construct paths. I keep such code in a global template header that's used for all the pages in my application. There really are only a very small number of things like that to worry about; in my application I think there are like 4 or 5 items of information like that to communicate with included .js files.
Solution, in case anyone else need it.
I put the following code in my theme level index.php file
<script>
window.themePath = "<?php echo $site_info[theme_style_path]; ?>";
</script>
And use the javascript global variable to append the path.
The source of the Javascript is irrelevant for the purposes of HTML being embedded within the page. What matters is the location of the page the HTML is embedded within.
For instance, if you load a page from example.com/index.php, and some Javascript loaded from example.com/js/script.js inserts some HTML into the main page, then image path references are going to be relative to /, as that's where the page was loaded from, not from /js.
As for embedding PHP in JS, you're right - but you can have PHP emit a .js file trivially:
<?php
header('Content-type: text/javascript');
?>
var settings = <?php echo json_encode($SETTINGS_ARRAY); ?>;
would generate perfectly valid Javascript, even thought it came from a PHP file.
Related
Actually i am writting a php script to link some javascript files in my multiple sites.
Example : site_dir1/js/jquery.1.4.2.js and
site_dir2/js/jquery.1.4.2.js
In this case, in the document_root location i have a file called "jquery/jquery.inc.php which has the follwing code
$jq_142 = "<script type='text/javascript' language='JavaScript' src='jquery-1.4.2.min.js'></script>";
I will use this code to make require my php file anywhere i want
<?php require $_SERVER['DOCUMENT_ROOT'].'/jquery/jquery.inc.php' ?>
And the reputation of jquery file keeps only one copy in my following location.
$_SERVER['DOCUMENT_ROOT'].'/jquery'
My doubt is still my javascript files are not loaded where ever i call the files.
I am little messed. how to fix it? any better solution?
If you're going to do this you need to give the script an absolute path rather than a relative one.
When you include it in index.php, the browser will look at /jquery.js for the file. When you include it in /folder/page.php, the browser will look check /folder/jquery.js.
See how it changes depending on where you are in the website? You need to tell it to look at a specific place, like:
$jq_142 = "<script type='text/javascript' language='JavaScript' src='http://example.com/scripts/jquery-1.4.2.min.js'></script>";
This means that no matter where you are in your site, the browser will check http://example.com/scripts/jquery-1.4.2.min.js for jQuery.
Finally, I found my page.php script have loaded my jquery.inc.php file. But the jquery file linking script has been coded inside quotes as follow :
$jq_142 = "<script type='text/javascript' language='JavaScript' src='jquery-1.4.2.min.js'></script>";
So it's couldn't be displayed. And i found this jquery.inc.php is not called by .html files. Only for .php files. I couldn't find any alternative solution for this problem.
I have linked by using the absolute path of my unreplicated jquery library files from jquery/jquery-xxx.js directly.
I've got this javascript code:
$(document)
.load(function(){
$.post(
'result_source.php?term='+<?php echo $_REQUEST['term']; ?>
);alert('abc123');
});
and it doesn't alert('abc123');. If I remove the
+<?php echo $_REQUEST['term']; ?>
it does alert('abc123').
Thanks
You need to take the PHP part out of the concatenation. The PHP is effectively pasted in to the javascript page before it is processed, so unless your $_REQUEST['term'] is the name of a javascript variable you are using, it will cause errors.
Change it to: $(document).load(function(){$.post('result_source.php?term=<?php echo $_REQUEST['term']; ?>');alert('abc123');});
Bear in mind this won't work inside external javascript files, unless you create an .htaccess or something to configure the server so it parses .js files as PHP before outputting to the browser
PHP will not run in an external JavaScript file unless you create a .htaccess file or configure the server so it parses .js files as PHP before outputting to the browser.
If you put that in a file(with a .php extension), in <script> tags, it will work, though.
To give you more context, my file structure look like: includes/overall/footer.php and in this file I am referencing js/nav-header.js note that both includes and js folders are in the base folder. Am I doing this correctly?
includes/overall/footer.php:
<?php include 'includes/footer.php'; ?>
<script type="text/javascript" src="libs/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="libs/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" scr="js/nav-header.js"></script>
</body>
</html>
This file itself is included in another file, does it have something to do with the order in which things are being loaded perhaps? Reason being is that I cannot seem to get anything in the javascript file to fire. Any help would be appreciated.
What dose you nav-header.js contain?
Assets are requested from the server in the order they appear in the html. So nav-header.js will be called after jquery however that doesn't mean it will finish loading before it.
Also to load files use absolute paths:
/js/nav-header.js instead of js/nav-header.js
that way you know the js/nav-header.js will be loaded from the root folder.
if u have code in js/nav-header.js that fires on load(not on document ready) i would sugest moving it to document ready.
I recommend against using hardcoded absolute paths, as that will make the application less portable.
Instead, set a $base_url variable that you can echo in your html
<?php $base_url = "/"; ?>
In your html
<script type="text/javascript" scr="<?php echo $base_url; ?>js/nav-header.js"></script>
You can use this throughout your site for js, css, anchors, etc.
Use the BASE tag in HTML with all relative paths :D
Phil Sturgeon wrote a blog post about its use in CodeIgniter, but essentially you don't need to use a framework to make use of the same principle.
You should store the document root in a variable somewhere in your application and have a function that references it, then echo that out into the base tag. This way when you move your application to a different environment you only have to change the root in one place and you are good to go.
Also, read this before using the base tag.
During a site scraping, I discovered several scraped functions in JavaScript that I need to modify because the code uses a relative path:
/UserControl/bla
I need to modify it to use absolute path:
www.domain-name.com/UserControl/bla
The problem is, those functions written in a separate file included by the scraped page. So far I can only stream that file using the PHP function file_get_contents(), change the part I need using preg_replace, and insert that script in the head section of the scraped HTML. I don't have access to modify the included JavaScript file because it's on a server I don't have access to.
Is that the right way to do this?
What I do in this cases is to declare JavaScript global variables with the objective to be constant values, then, I can access this variables from my included JS files, for example:
<script>
Globals = {
absoluteUrlPrefix: "<?= getAbsoluteUrlPrefix(); ?>"
};
</script>
<script src="myjsfile.js"></script>
on myjsfile.js
...
var absoluteUrl = Globals.absoluteUrlPrefix+"/UserControl/bla";
...
preg_replace is an option, if it's just to show the web pages on your machine, you could also insert a base-path tag:
http://www.w3schools.com/tags/tag_base.asp
I know we can get some path with <?php bloginfo('something');?> into php files, but is it some equivalent for javascript file loaded with the enqueue_script function ?
Did wordpress change some shortcode into those files ?
EDIT : I think I did not clearly express my needs. So i want to know if wordpress had some shortcode who, placed into a js file who is loaded with the enqueue method, will be replaced by the template path. Typically i need to make some ajax call form a .php file from my template and want to avoid hard linking file
No javascript files won't be parsed as php, and as such won't process any shortcodes or php.
Why not just make your links relative. Often I find subdomaining my dev copy, removes any problems when moving a site live and broken links.
You could cheat and link to a php file, which then passes header information as Javascript. Doesn't seem very elegant though. See here.
Or you could just declare the variable in a little bit of inline Javascript and pick it up in the external JS file.
<script type="text/javascript">
var siteURL= '<?php bloginfo('url');?>';
</script>
<script type="text/javascript" src="yourscript.js"></script>
Then in yourscript.js just reference the variable 'siteURL'
You have to register scripts using wp_register_script(). Then by placing wp_enqueue_script before wp_head() it will load them in for you. The idea of using wp_enqueue_script is that you don't need to enter them all in manually, and you can load other scripts depending on whether a certain script has been loaded.