开发者

Edit the <title> tag with jQuery or PHP

开发者 https://www.devze.com 2023-04-10 04:08 出处:网络
I have some home made blog, and when viewing blog posts I\'d like to change the title tag for adding the post title, so when I use the Twitter\'s tweet button, it adds the new button. I tried:

I have some home made blog, and when viewing blog posts I'd like to change the title tag for adding the post title, so when I use the Twitter's tweet button, it adds the new button. I tried:

<script type="text/javascript">

 $(document).ready(function() {

 $(this).attr("title", "<?php echo $tagValue["title"]." - YourCloud"; ?>");

});
</script>

And:

 $(document).ready(function() {

 document.title = "<?php echo $tagValue["title"]." - YourCloud"; ?>";

});
</script>

But the Tweet Button still has the title I set up at the start, how can I fix this?

EDIT: In my index.php I add the title tag, I want 开发者_开发技巧to modify it so when you see the source it already appears modified


I think you want to set the text of the tweet, because by default the document title is tweeted. Well, you can modify the tweet button code to achieve this. See the href of the anchor element below where I added a 'text' parameter.

<a href="https://twitter.com/share?text=any_text_you_want" class="twitter-share-button">Tweet</a>

More here- https://dev.twitter.com/docs/tweet-button


Try instead

$("title").text("<?php echo $tagValue['title']." - YourCloud"; ?>");


$("title").html("<?php echo $tagValue['title']." - YourCloud"; ?>");


document.title is what you should use to change the title of the page, f.ex:

document.title = 'Foo bar';

But it sounds like you are trying to change the title of the tweet button?


First of all, yes I know this has an accepted answer - but I wasn't sure if it was exactly what he was after.

I'm not sure that anyone understands your questions properly - and after reading through the question/answers I'm still non the wiser.

Also, document.title over jQuery in this instance.

You're saying that $("title").html("<?php echo $tagValue['title']." - YourCloud"; ?>"); is showing the original title? Well of course it is. All the PHP is executed BEFORE the JavaScript, so the echo $tagValue['title'] is essentially a static value throughout your JavaScript. Therefore, you're loading the same title into the title bar every time you try and change it.

By the time your PHP reaches the browser, it's no longer $("title").html("<?php echo $tagValue['title']." - YourCloud"; ?>");, it's $("title").html("My Title - YourCloud");

Unless of course I'm understanding the question wrong. You really are missing so many details. Where is $tagValue['title'] being set - what is the value. I'm presuming that PHP is parsing your JS correctly so I won't bother asking whether you're using an external script. What do you expect the answer to be.

But from everything I've read and understood from your script - why are you changing the title with JavaScript at all. If PHP is putting a static value into your page, then you may as well just put <title><?php echo $tagValue['title']." - YourCloud"; ?></title> in the head of your HTML. The only reason that I can see for the title changing with JavaScript is when an event happens that the user needs notifying about - or if you're using something like Ajax to have a dynamic site.

I'll update this post with more details if you can comment back and, well, help me/everyone to understand your question.

EDIT: If you want to edit the title by pressing a button: Why not place an onclick on the button itself and just do something like:

function test() {
 if(document.title.indexOf(" - YourCloud")==-1){
  document.title += " - YourCloud";
 }
}

Not tested, but, surely that could work for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消