Putting arbitrary inline Javascript in the header with addHeaderItem - possible?
Permalink
Hi all
I'm attempting to output some javascript variables into the header of a page which uses a custom block I've built. I've created a string and tried to output it in the on_page_view() function of the block's controller, something like this:
But just doesn't work right.. well, it outputs this to the header:
so basically nothing in that loop gets added to the string, and it outputs only the initial instantiation of the string.
Any thoughts on this?
I'm attempting to output some javascript variables into the header of a page which uses a custom block I've built. I've created a string and tried to output it in the on_page_view() function of the block's controller, something like this:
$descriptions = '<script type="text/javascript">var myvar = ['; foreach ($descs as $desc) { $descriptions .= '"' . $desc["name"] . '",'; } $descriptions = substr($descriptions,0,-1) . ']</script>'; $this->addHeaderItem($descriptions);
But just doesn't work right.. well, it outputs this to the header:
<script type="text/javascript">var simple_markers_descriptions = [
so basically nothing in that loop gets added to the string, and it outputs only the initial instantiation of the string.
Any thoughts on this?
![mesuva](/files/avatars/45566.jpg)
It could be fine, but I've not seen foreach loops formatted like that, they would normally have parenthesis around the parameters, e.g.
Sorry, I mistyped the code - it does have parentheses (have updated the OP).
Solved: I was retrieving the array incorrectly - it should have been $this->arrayName instead of just $arrayName.
Silly me!
Silly me!
Can you also post the code that is populating the $descs variable? It could be that the problem is there. Maybe it's not being created as an array? Or it's not getting populated?
- John
- John
Woops... we must have been posting simultaneously... I just saw that you solved it AFTER my post appeared.
Glad you got it working!
- John
Glad you got it working!
- John
Thanks for replying though, you were on the right track. I should have been more thorough before posting. Will be more patient and rub my eyes before another trawl through the code before I post next time!
LOL! No problem. I've done the same! Spent hours trolling through code trying to figure out what's wrong... it gets late into the night... I post a question right before shutting down and going to bed... get an inspirational thought as my head is hitting the pillow (or right when I wake up in the morning)... then have to post the "mea culpa" for having asked for help when all I really needed was sleep! ;D
So you're not alone! LOL!
Glad it's working.
- John
So you're not alone! LOL!
Glad it's working.
- John