Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
JS + PHP Document Title Change
Old 10-12-2010, 01:47 PM JS + PHP Document Title Change
Junior Talker

Posts: 1
Name: Reece Benson
Trades: 0
Hi!

I've just come to ask as I am making a website.. and well, I use PHP to get the pages title. Example:

Code:
	include('../../includes/core.php');
	define("PAGENAME", "Random Area");
	$page_name = SITESHORT . SEP . PAGENAME;
(Ignore the SITESHORT and SEP, they are already predefined variables set in core.php)

What I am trying to do, is when the menu item is clicked, the page loads in the div, and the name of the title changes to the page which is in the PHP file which is loaded into the div. (That is probably the weirdest way of putting it)

Example:
Code:
	<head>
		<script type="text/javascript">
			function setNewTitle( name ) {
				document.title = name;
			}
			function setPageTitle() {
				var $title = "<?php echo $page_name; ?>";
				setNewTitle($title);
			}
		</script>
	</head>
	<body onload="javascript:setPageTitle();">
	</body>
As you can see in the above example, i'm trying to get the PHP variable '$page_name' to be put into the var I created in the javascript (maybe i'm setting the var wrong..? not very good with javascript)

Sum:
- Make PHP variables
- Javascript function tries to execute php variable
- Fails

Anybody out there who could help me?
Thanks in advance,
iBenson
iBenson is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-14-2010, 09:31 AM Re: JS + PHP Document Title Change
captkrackerjack's Avatar
Experienced Talker

Latest Blog Post:
Moved Blog to NGINX!
Posts: 31
Name: Greg
Location: California
Trades: 0
Is your page code linked to the PHP file which sets the variable? It seems you only posted the portions that seems to be causing the problem, but I can't see if its possibly something else. Just by looking you have to have the $page_name PHP variable available so that the echo $page_name actually has a string to echo otherwise your probably getting a page title with nothing.
__________________

Please login or register to view this content. Registration is FREE

Fav sites; last.fm, mashable.com, speckyboy.com
captkrackerjack is offline
Reply With Quote
View Public Profile Visit captkrackerjack's homepage!
 
Old 10-14-2010, 11:07 AM Re: JS + PHP Document Title Change
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Why would you set the page title with JavaScript, when you could just output it directly into the HTML?
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 10-14-2010, 11:18 AM Re: JS + PHP Document Title Change
captkrackerjack's Avatar
Experienced Talker

Latest Blog Post:
Moved Blog to NGINX!
Posts: 31
Name: Greg
Location: California
Trades: 0
Quote:
Originally Posted by wayfarer07 View Post
Why would you set the page title with JavaScript, when you could just output it directly into the HTML?
From what he said he is not leaving the page. He is just loading a div on the same page so he is changing the title to match the conent of the div. Probably using JavaScript and or CSS.
__________________

Please login or register to view this content. Registration is FREE

Fav sites; last.fm, mashable.com, speckyboy.com
captkrackerjack is offline
Reply With Quote
View Public Profile Visit captkrackerjack's homepage!
 
Old 10-14-2010, 11:19 AM Re: JS + PHP Document Title Change
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Maybe I'm not understanding... do you have multiple "pages" that are loading on a single page, through AJAX or something? If so, the code you've given as an example, while not wrong in its own right, will only execute ONCE (when the whole HTML document is loaded the first time).
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.

Last edited by wayfarer07; 10-14-2010 at 11:20 AM..
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 10-14-2010, 11:32 AM Re: JS + PHP Document Title Change
captkrackerjack's Avatar
Experienced Talker

Latest Blog Post:
Moved Blog to NGINX!
Posts: 31
Name: Greg
Location: California
Trades: 0
I don't know what he is trying to accomplish exactly but from what iBenson said

Quote:
What I am trying to do, is when the menu item is clicked, the page loads in the div, and the name of the title changes to the page which is in the PHP file which is loaded into the div. (That is probably the weirdest way of putting it)
It seems as if he is trying to use AJAX or some type of trickery to change the title once the content is place in the div, I would imagine AJAX so then the title changes to match what is displayed.

But you are correct about the code only executing once. And as an aside I wouldn't use JavaScript to set a title after page load anyways even with AJAX content loading.
__________________

Please login or register to view this content. Registration is FREE

Fav sites; last.fm, mashable.com, speckyboy.com
captkrackerjack is offline
Reply With Quote
View Public Profile Visit captkrackerjack's homepage!
 
Reply     « Reply to JS + PHP Document Title Change
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23298 seconds with 12 queries