2

I am trying to access a global variable I created using javascript within a content node i created. In an attached js file i have var rootpath = "/clients/UMP/";

Inside of my drupal page i have:

<script>document.write(window.rootpath);</script>

But it just shows the text: undefined

Am i doing something wrong? Is there a more better way to do it using php perhaps? Thank you for taking the time to read this and respond. I apologize for being such a noob with drupal. I have only been using it for a few weeks.

4
  • What are you trying to do exactly? Depending on what you need there is probably a better alternative to writing global vars directly in your node. Commented May 14, 2015 at 22:57
  • I have a preview server and a live server. I am trying to make it so the path to the images and ect change across the whole site by changing the global root path variable so we can easily go from preview to deploying it live on the other server which has another root path to the drupal installation. Commented May 14, 2015 at 22:59
  • I wouldn't think that would be an issue if you are using relative URL's unless image1.jpg changes folders between servers ie.. devserver.com/images/image1.jpg and liveserver.com/anotherfolder/image1.jpg. Are you using absolute URL's for all your resources? Commented May 14, 2015 at 23:09
  • I tried relative url pathing but it wasnt working for me, maybe i was doing it wrong. For example this image is relative to the drupal root directory however it just renders out as http:///sites/default/files/pictures/apple-logo.png instead of (site url root directory)/sites/default/files/pictures/apple-logo.png<img src="/sites/default/files/pictures/apple-logo.png"> Because of this i used absolute urls instead. Commented May 14, 2015 at 23:23

1 Answer 1

0

Based on your comments, you might want to try using Drupals built in functions to retrieve the correct path for your assets.

The 2 you could take a look at are base_path() and path_to_theme().

For example:

<img src="<?php print base_path(); print path_to_theme()?>/images/image1.jpg" />

4
  • Thank You! I would vote you up but i don't have 15 rep. Commented May 14, 2015 at 23:39
  • Now i'm running into another issue of where the php isnt executing i guess. It just picks up the url as being GET http://example.com/UMP/%3C?php%20print%20base_path();?%3E/sites/default/files/pictures/apple-logo.png 404 (Not Found) My code is <img src="<?php print base_path();?>/sites/default/files/pictures/apple-logo.png"> I have it set to full html and php execution is enabled... Commented May 14, 2015 at 23:59
  • Full html wont run php by default, you'll need to enable the php format permissions and set the format to php. You should be able to 'accept this answer' if it helped since you can't upvote Commented May 15, 2015 at 0:16
  • doesn't give me an option anywhere to "accept this answer". Might require multiple answers to do it or something or more reputation points. Commented May 15, 2015 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.