eZ Community » Forums » Setup & design » ezscript_require external resource?
expandshrink

ezscript_require external resource?

ezscript_require external resource?

Thursday 17 March 2011 6:05:44 pm - 14 replies

Hello,

I wanted to load Google Maps JS file as a remote URL like this:

{ezscript_require('http://maps.google.com/maps/api/js?sensor=true')}

The problem it loads the content as JS string inside a script tag.

<script type="text/javascript">http://maps.google.com/maps/api/js?sensor=true</script>

And I expect :

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>

Is there a way to achieve that, without having to code anything? (it would be crazy for this feature only).

Thanks for your concern happy.gif Emoticon

Thursday 17 March 2011 8:03:54 pm

Hi Thomas,

As far as I can tell, ezscript_* template operators can not handle remote JavaScript files. I guess you call ezscript_require in some special view templates to call Google Maps JavaScript only when it's needed ? In this case it would make sense, I think you should open a enhancement request in the issue tracker.

As a workaround, you can define a persistent variable in your view template and then include remote JavaScript file(s) based on it. It's just a matter of some lines of template... sorry for the without having to code anything blunk.gif Emoticon

Cheers

Thursday 17 March 2011 8:22:59 pm

Well, but ezscript_require also load jquery from google if you load it via ezjsc::jquery, right?

so, i think you can add a custom ezjscore server function and from there, output your needed javascript code.

i mean, something like https://github.com/ezsystems/ezjscore/blob/master/classes/ezjscserverfunctionsjs.php#L231

Modified on Thursday 17 March 2011 8:43:59 pm by Carlos Revillo

Thursday 17 March 2011 9:01:33 pm

Thomas, the fact is that ezscript_* template operators want your parameter to end in ".js". So by doing this little trick:

{ezscript('http://maps.google.com/maps/api/js?sensor=true&amp;ext=.js')}

it should work. (it worked on my site)

A note to the ezjscore author:

Why is it reqired for the parameters to end in ".js"? Could we avoid this requirement?

Thursday 17 March 2011 11:43:33 pm

@Carlos : you're totally right, I've totally forgot about that feature. In this case, you'll just have to ignore LoadFromCDN setting though.

@Marko : wow that seems tricky happy.gif Emoticon I've just tried but it does not seem to work for me on an eZ Publish 4.4 with the bundled ezjscore nor with the last git version.

Cheers

Friday 18 March 2011 8:03:49 am

I had the same problem for my Google Custom Search Engine usage : http://recherche.fedora-fr.org/

I have used jquery for that :

$("head").prepend($('<link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" />') );

Friday 18 March 2011 10:47:19 am

@Marko : wow that seems tricky happy.gif Emoticon I've just tried but it does not seem to work for me on an eZ Publish 4.4 with the bundled ezjscore nor with the last git version.

First I did try it on eZP 4.5, but now I have tried it on 4.4 and it works too. It woks both with LoadFromCDN enabled or disabled.

Are you sure you did the exactly same thing as I did?

And there's an interesting detail...

When I enable the Packer in INI settings, and call:

{ezscript('http://maps.google.com/maps/api/js?sensor=true&amp;ext=.js')}

by itself, the the content of http://maps.google.com/maps/api/js?sensor=true if not included in the packed file nor it's included via the <script> tag in HTML.

But if I put http://maps.google.com/maps/api/js?sensor=true together with all other JS scripts in one ezscript() call, then it is included correctly via the <script> tag:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&amp;ext=.js" charset="utf-8"></script>

Hm, maybe Andre R. could comment some more on this one...

Friday 18 March 2011 10:48:43 am

I had the same problem for my Google Custom Search Engine usage : http://recherche.fedora-fr.org/

I have used jquery for that :

$("head").prepend($('<link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" />') );

Why did you use jquery for that?

You could have just put:

<link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" />

in yout HTML without any procesing by eZP.

Friday 18 March 2011 11:21:28 am

@Marko : the packer was enabled when doing my tests. I guess Guillaume uses jQuery to be able to embed the stylesheet depeding on the page. His JavaScript line is probably generated in the full view of some special nodes, the template generates this code to include the stylesheet only when it's needed.

Cheers

Modified on Friday 18 March 2011 11:21:55 am by Damien Pobel

Friday 18 March 2011 12:05:19 pm

I guess Guillaume uses jQuery to be able to embed the stylesheet depeding on the page. His JavaScript line is probably generated in the full view of some special nodes, the template generates this code to include the stylesheet only when it's needed.

Yes, but think about it, there's still no need for using jQuery...

Friday 18 March 2011 2:11:01 pm

Thanks for your replies happy.gif Emoticon

Indeed, yes I wanted to load gmap only for pages requiring it; not on all the site webpages.

I load the script asynchroneously so far (within a local JS) but there are many cases where you can call a remote script without an ending .js extension, for JS scripts or JSON-P response (like YQL, Twitter API or Webservices).

I guess the extension should check for an URI scheme, or at least, a "//" (no scheme = same protocol).

Friday 18 March 2011 2:52:43 pm

Hi,

Should you think what you are blocked by is a bug or would benefit from a request for enhancement on eZJSCore, i encourage you to report it there : http://issues.ez.no/ezpublish (select the right component in the right-column : eZJSCore).

Cheers,

Modified on Friday 18 March 2011 2:53:10 pm by Nicolas Pastorino

Friday 18 March 2011 4:48:19 pm

> Why is it reqired for the parameters to end in ".js"? Could we avoid this requirement?

Yes, issue created: http://issues.ez.no/IssueView.php?Id=18167&activeItem=1

Workaround is (as mentioned) to append ".js" using querystring.

Modified on Saturday 19 March 2011 9:58:29 am by André R

Monday 21 March 2011 4:09:50 pm

Added patch to issue, feel free to test for your cases it so we can get this closed.

Wednesday 23 March 2011 11:08:33 am

Thanks for the patch, it works well for me !

I can now clean all the ugly code...

{* TODO: load this in the <head> section *}
{run-once}
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
{/run-once}

thanks again

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from