1

I want to use jquery so i can add dynamically the "store_name" data of the code bellow inside the page title attribute for seo purposes.

<?php 
$store_info=array(   
'store_id'=>$store_id ,
'phone'=>$data['phone'],
'store_name'=>$data['store_name'],
);
?>

I already tried to use this code:

<script src="http://code.jquery.com/jquery-1.11.0.min.js">
$(function(){
$(document).attr('title', $data['store_name']);
});
</script>

but didn't work at all. What in the world i'm doing wrong ?

People mentioned that already there is a similar question here but this isn't what i 'm looking for. In my question i want to pass in the title attribute, data from a php array as i described above.

Designer
  • 875
  • 7
  • 26
  • Possible duplicate of [jQuery: how to change title of document during .ready()?](https://stackoverflow.com/questions/180103/jquery-how-to-change-title-of-document-during-ready) – Tomasz Lloyd Jun 28 '18 at 16:50
  • It's not the same. In my question the problem is that i cant get and display inside the title attribute some data from a php array. Thanks – Designer Jun 28 '18 at 17:08
  • Is there a reason you're using `$data['store_name']` and not `$store_info['store_name']` ? – Tomasz Lloyd Jun 28 '18 at 17:11
  • Nope there isnt any important reason but i already tried to use $store_info['store_name'] and didn't work either. However if i use it anywhere in the page it work fine and i can get the store name except of adding it to the title attribute of the page !!! – Designer Jun 28 '18 at 17:15
  • You can try this https://stackoverflow.com/questions/50960780/how-to-match-half-url-using-switch-case-for-dynamic-page-title-in-php – Vinay Jun 28 '18 at 17:32

1 Answers1

0

It took me a moment to figure this out, and then it hit me:

Remove the src="http://code.jquery.com/jquery-1.11.0.min.js" inside your script tag, since that's ignoring everything inside the tag itself. You'll then need two script tags:

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<?php
    $store_info['store_name'] = 'new_title';
    echo '<script type="text/javascript">'.
        '$(function(){$(document).attr("title", "' . 
        $store_info['store_name'] . 
        '" );});</script>'
?>

From the w3.org docs:

If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI.

Tomasz Lloyd
  • 491
  • 3
  • 12
  • 1
    Thanks for your answer @Tomasz Foster but didnt work. I checked the console and i caught the error "ReferenceError: $data is not defined" How is this possible? Anywhere in the page i use i am getting the name of the store with out problem. – Designer Jun 28 '18 at 17:38
  • Right, sorry, use your `$store_info['store_name'];` instead of the `$data` variable. I'll update my answer to reflect that – Tomasz Lloyd Jun 28 '18 at 17:39
  • 1
    Same results with $store_info['store_name'] :(( I test the code inside the whole page and works fine but when i use it inside the jquery it gives "ReferenceError: $store_info is not defined" !!!! Very frustrating :( – Designer Jun 28 '18 at 17:44
  • 1
    Agh, duh. You'll have to output the php variable, not treat it as a javascript variable. I'll update my answer. – Tomasz Lloyd Jun 28 '18 at 17:45
  • 1
    Thank you. It worked perfect. You were very helpful @Tomasz Foster. – Designer Jun 29 '18 at 09:17