vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Pass a vBulletin variable to Javascript? (https://vborg.vbsupport.ru/showthread.php?t=264472)

TWood 05-31-2011 01:40 AM

Pass a vBulletin variable to Javascript?
 
There is a vBulletin variable called {vb:raw criteriaDisplay} that can be called into a template just by naming it:

Code:

<p>{vb:raw criteriaDisplay}</p>
will show the current value. The current value is the Tag search term that is used to create the tag search results. (I edited out the "Tag:" part in the PHP file that creates it, so your Tag search result list may include that part.)

I need to access that variable within a Javascript on the same page that calls in ads based on its value. So far we haven't been able to pass the variable to Javascript simply by calling it. One of the comments at DevShed said:

Quote:

"... you probably won't be able to use {vb: raw criteriaDisplay} to determine the number that needs to go there. You need to have this number available before the template is rendered ..."
So, how can I make {vb: raw criteriaDisplay} available to a Javascript on the page, before the template is rendered?

Thanx

kh99 05-31-2011 01:47 AM

I don't understand the quote in your post. If the number appears in the page then it's available before the template is rendered (unless you're talking about a different template?).

In any case, have you tried putting this in the template?

HTML Code:

<script type="text/javascript">
var criteriaDisplay = "{vb:raw criteriaDisplay}";
</script>


TWood 05-31-2011 02:11 AM

Thanks kh99 -

Adding that script yields this in View Source:

Code:

<script type="text/javascript">
var criteriaDisplay = "";
</script>

Which is the same problem in the main Javascript, it's just not seeing the variable. There should be a number there between the quotes that matches the number that is shown on the page where I have <p>{vb:raw criteriaDisplay}</p>. It calls the variable fine outside of Javascript.

Regarding the quote, yes it's the same page/template.

kh99 05-31-2011 02:28 AM

Hmm...that's strange - it works for me. I had this set up to test soimething else:

In a plugin at global_start
Code:

$criteriaDisplay = 5;
vB_Template::preRegister('header',array('criteriaDisplay' => $criteriaDisplay));


then in the header template:
HTML Code:

<script type="text/javascript">
var criteriaDisplay = "{vb:raw criteriaDisplay}";
</script>


and in the html source:
HTML Code:

<script type="text/javascript">
var criteriaDisplay = "5";
</script>


ETA: I have version 4.1.3, maybe there was some problem before that version.

TWood 05-31-2011 02:35 AM

Weird... I'm also using 4.1.3

Would it be okay to create a plugin that had just this part at global_start:

Code:

vB_Template::preRegister('header',array('criteriaDisplay' => $criteriaDisplay));

I actually have the Javascript in headinclude, if that makes any difference. So the plugin would be:

Code:

vB_Template::preRegister('headinclude',array('criteriaDisplay' => $criteriaDisplay));

kh99 05-31-2011 02:47 AM

Yeah, that should be OK. But if it's coming out in the <p>{vb:raw criteriaDisplay}</p> in the same template (headinclude) then whatever you're doing must already be makign the variable available. (if you're putting that in a different template then the problem might be that $criteriaDisplay isn't set yet when header is rendered).

I just noticed that there's already a bunch of js vars defined the same way in headinclude, so I don't know why yours wouldn't work the same way.

TWood 05-31-2011 02:57 AM

My bad, actually the <p>{vb:raw criteriaDisplay}</p> is in the searchresult_list template, which comes after the header info. So by then it's available. I'll try the plugin.

kh99 05-31-2011 03:01 AM

Ah...that's probably it. But the plugin won't help unless the $criteriaDisplay variable has a value set (not just preregistered) before the header is rendered. That might be what the DevShed person was trying to get at. You might have to juggle things around to get it to work out.

TWood 05-31-2011 03:05 AM

Yeah, no go on the plugin. I'll try moving the Javascript for the ads to the searchresult_list template. I know they want it in the header so it's global, but it just won't do it.

--------------- Added [DATE]1306815435[/DATE] at [TIME]1306815435[/TIME] ---------------

Success!

Moving the ad code down to the searchresult_list template is all it needed.

kh99 - Thank you for your help!


All times are GMT. The time now is 03:28 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.00954 seconds
  • Memory Usage 1,738KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (5)bbcode_code_printable
  • (3)bbcode_html_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (9)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete