Jump to content


Photo

Passing (PW)variables to jquery script

solved jquery

  • Please log in to reply
13 replies to this topic

#1 formmailer

formmailer

    Sr. Member

  • Members
  • PipPipPipPip
  • 245 posts
  • 28

  • LocationHudiksvall, Sweden (but originally from The Netherlands)

Posted 11 March 2012 - 03:15 AM

Hi,

This might be a dumb question, but my jquery skills are still somewhere below zero. ???
I do have the following jquery code in my external .js file. And it works fine, but, for maintenance and reusebility reasons, I don't like having the target url hard coded in the .js file.
But I assume it isn't possible to pass the PW api $config->urls->root to this external .js file, would it?

/*Make header div clickable*/
$(document).ready(function() {
  $('#header').click(function(){
			window.location = 'http://www.mysite.com/';
  });
  $('#search_form').click(function(event){
	event.stopPropagation();
	});
});
/*End header div*/

/Jasper

#2 diogo

diogo

    Hero Member

  • Moderators
  • 1,982 posts
  • 1061

  • LocationPorto, Portugal

Posted 11 March 2012 - 04:17 AM

well, you can... just put <?include(yourfile.js);?> between <script> tags ;)

#3 adamkiss

adamkiss

    Master of the universe

  • Moderators
  • 1,078 posts
  • 289

Posted 11 March 2012 - 06:48 AM

The simplest way is to create PHP enhanced javascript; Rename the file from .js to .js.php, and change it like this:


/*Make header div clickable*/
$(document).ready(function() {
  $('#header').click(function(){
                        window.location = '<?php echo $config->urls->root; ?>';
  });
  $('#search_form').click(function(event){
        event.stopPropagation();
        });
});
/*End header div*/

And include src="myscript.js.php";

Alternatively, you should be able to do this like this:

//...snip
  window.location = '/';
//...snip

Alternatively, you could (and I would totally do this) switch your HTML, so everything that should go to index will be normal link - a

<header><a href="<?php echo $config->urls->root; ?>">
  <!-- your data here -->
</a></header>

:)

#4 diogo

diogo

    Hero Member

  • Moderators
  • 1,982 posts
  • 1061

  • LocationPorto, Portugal

Posted 11 March 2012 - 07:02 AM

Rename the file from .js to .js.php

Ok, if the js file stays in the assets folder, you really should do this...

#5 Soma

Soma

    Hero Member

  • Moderators
  • 3,186 posts
  • 1743

  • LocationSH, Switzerland

Posted 11 March 2012 - 07:02 AM

You could output a json config object using php , then it will be available in your js.

before your scripts in the header:

<?php
$jsconfig = array(
	'root' => $config->urls->root,
	'domain' => $config->httpHost
);
echo "<script>var config = " . json_encode($jsconfig) . ";</script>";
?>

this will output an json object

<script>var config = {"root":"\/","domain":"pw2-dev.ch"};</script>


you can access in js like this

<script>
$(document).ready(function() {
  $('#header').click(function(){
	  window.location = "http://"+config.domain;;
  });
  $('#search_form').click(function(event){
	   event.stopPropagation();
   });
});
url = config.root;
</script>

This is the most easy and convenient way for me.

@somartist | modules created | support me, flattr my work flattr.com


#6 adamkiss

adamkiss

    Master of the universe

  • Moderators
  • 1,078 posts
  • 289

Posted 11 March 2012 - 07:03 AM

Ok, if the js file stays in the assets folder, you really should do this...


Not only that, but if you have php code in your javascript files, server has to know to run it through PHP before serving to browser :)

#7 diogo

diogo

    Hero Member

  • Moderators
  • 1,982 posts
  • 1061

  • LocationPorto, Portugal

Posted 11 March 2012 - 07:09 AM

Not only that, but if you have php code in your javascript files, server has to know to run it through PHP before serving to browser

Not if you include it with php... in this case, (.js) wouldn't be any different from (.inc)

#8 adamkiss

adamkiss

    Master of the universe

  • Moderators
  • 1,078 posts
  • 289

Posted 11 March 2012 - 07:18 AM

Not if you include it with php... in this case, (.js) wouldn't be any different from (.inc)


That is true, sorry. I did not know that :)

#9 diogo

diogo

    Hero Member

  • Moderators
  • 1,982 posts
  • 1061

  • LocationPorto, Portugal

Posted 11 March 2012 - 07:25 AM

Still, it's a good idea to rename it to .php if it will be in a not protected folder. Never to much to remind :)

#10 formmailer

formmailer

    Sr. Member

  • Members
  • PipPipPipPip
  • 245 posts
  • 28

  • LocationHudiksvall, Sweden (but originally from The Netherlands)

Posted 11 March 2012 - 03:52 PM

Thanks for your replies, guys!

I didn't think of including the .js file, but since I'd like to have as little inline javascript as possible (for performance reasons), I didn't choose this method. I didn't know that you could php-enable javascript files. This is good to know for the future.

Alternatively, you should be able to do this like this:

//...snip
  window.location = '/';
//...snip

I also thought about this, but on the development server, this site is running in a subdirectory, so the link wouldn't work there. Not a big issue, but nice to avoid. :)

Alternatively, you could (and I would totally do this) switch your HTML, so everything that should go to index will be normal link - a

<header><a href="<?php echo $config->urls->root; ?>">
  <!-- your data here -->
</a></header>

The problem here is that the div doesn't contain anything.... it's just a background.

I ended up using Soma's solution. It works great.

Thanks again to all of you! 8)

/Jasper

#11 adamkiss

adamkiss

    Master of the universe

  • Moderators
  • 1,078 posts
  • 289

Posted 11 March 2012 - 04:34 PM

The problem here is that the div doesn't contain anything.... it's just a background.


Then it actually totally doesn't have to be header... just make it a.header and you're all set. It seems to me that you do not know this (pardon me if you do), but with 'display:block;', link ('a') will act as if it was just another div, so I would go for it - it will work the very same way it does now without any javascript.

Note: If it will seem I push it too hard, it's only because I always try to go for as little JS as possible, and this is one of those rare moments when using pure HTML instead of JS is dead simple.

#12 diogo

diogo

    Hero Member

  • Moderators
  • 1,982 posts
  • 1061

  • LocationPorto, Portugal

Posted 11 March 2012 - 04:53 PM

I agree with Adam, no plausible reason for js here

#13 ryan

ryan

    Hero Member

  • Administrators
  • 5,771 posts
  • 3108

  • LocationAtlanta, GA

Posted 12 March 2012 - 08:43 AM

You could output a json config object using php , then it will be available in your js.


This is the route that ProcessWire takes with the admin too (you can see it by view source in browser when in PW admin).

PHP-powered JS files are definitely cool in the right places. But I worry a little about browser cache issues. You generally want your browser to cache those JS and CSS files. If you are outputting dynamic data with them, you may be preventing the browser from caching them, or battling it over caching them when you don't want it to. :) There are ways around it (managing cache headers and keeping track of when stuff changes) but it just seems like kind of a pain to me, though I've not done a lot of it.

#14 formmailer

formmailer

    Sr. Member

  • Members
  • PipPipPipPip
  • 245 posts
  • 28

  • LocationHudiksvall, Sweden (but originally from The Netherlands)

Posted 12 March 2012 - 09:11 AM

Then it actually totally doesn't have to be header... just make it a.header and you're all set. It seems to me that you do not know this (pardon me if you do), but with 'display:block;', link ('a') will act as if it was just another div, so I would go for it - it will work the very same way it does now without any javascript.

Note: If it will seem I push it too hard, it's only because I always try to go for as little JS as possible, and this is one of those rare moments when using pure HTML instead of JS is dead simple.


Thank you for your great suggestion, Adam. I actually knew that a.header with display:block: would behave like any other div. The only thing was that I didn't think of doing it that way until you suggested it. :)
I absolutely agree that it's better to avoid javascript when it isn't needed, like here.

But I am still glad I didn't think of this solution in the first place and just asked the question. I learned about PHP-powered javascripts, including .js in php files, json config objects and still ended up with the easiest and cleanest solution.
That's why I love this forum. Thanks guys! ^-^

/Jasper





Also tagged with one or more of these keywords: solved, jquery

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users