Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-08-2011, 03:35 AM   PM User | #1
JasonBeee
New to the CF scene

 
Join Date: Oct 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
JasonBeee is an unknown quantity at this point
If URL contains hashtag, then do this

If you go to thisiswhyimbroke.com and click any of the tabs, you'll see that an anchor/hash tag is added to each button. #a1,#a2, etc.

I want to use a script where if the URL contains #a1, then it echos certain HTML/PHP.

I was trying to use PHP for this but for some reason it can't detect anchors...

Any help is appreciated!
JasonBeee is offline   Reply With Quote
Old 10-08-2011, 05:49 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Not for "some reason". On purpose.

That hash isn't sent to the server-side code.

If you need the capability of a hash both server and client side, then hack it.

somesite.com/somepage.php?hash=a1

And then have the PHP code echo back
Code:
echo 'window.onload = function(){ location.hash="' . $_GET["hash"] . '"; };';
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 10-08-2011, 07:13 AM   PM User | #3
JasonBeee
New to the CF scene

 
Join Date: Oct 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
JasonBeee is an unknown quantity at this point
I actually got javascript to verify if there was a hashtag in the URL with this code:

Code:
<script type="text/javascript">
            $(document).ready(function () {
                if(window.location.href.indexOf("#a2") >= 0) 
                {
                  //output specific HTML
                }
});
        </script>
My only problem is I'm not sure how to outprint the HTML code I want there. I tried using a predefined PHP var, but to no success. Can anyone help me insert HTML with proper syntax? Here's the HTML for reference:

Code:
<ul class="tabset">
	<li class="active" id="tab1"><a href="#tab-1" class="tab">Random</a></li>
	<li id="tab2"><a href="#tab-2" class="tab">Price High</a></li>
	<li id="tab3"><a href="#tab-3" class="tab">Price Low</a></li>
	<li id="tab4"><a href="#tab-5" class="tab">Food &amp; Drink</a></li>
	<li id="tab5"><a href="#tab-6" class="tab">Home &amp; Office</a></li>
	<li id="tab6"><a href="#tab-8" class="tab">Gadgets</a></li>
	<li id="tab7"><a href="#tab-7" class="tab">Geek</a></li>
	<li id="tab8"><a href="#tab-4" class="tab">Latest</a></li>
	<li id="tab9"><a href="#tab-9" class="tab">All</a></li>

</ul>
JasonBeee is offline   Reply With Quote
Old 10-08-2011, 08:02 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
So what do you want to do?

Sounds like you want to click on the <li id="tab2"> ???
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 10-08-2011, 08:09 AM   PM User | #5
JasonBeee
New to the CF scene

 
Join Date: Oct 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
JasonBeee is an unknown quantity at this point
I just want to insert this HTML:
Code:
<ul class="tabset">
	<li class="active" id="tab1"><a href="#tab-1" class="tab">Random</a></li>
	<li id="tab2"><a href="#tab-2" class="tab">Price High</a></li>
	<li id="tab3"><a href="#tab-3" class="tab">Price Low</a></li>
	<li id="tab4"><a href="#tab-5" class="tab">Food &amp; Drink</a></li>
	<li id="tab5"><a href="#tab-6" class="tab">Home &amp; Office</a></li>
	<li id="tab6"><a href="#tab-8" class="tab">Gadgets</a></li>
	<li id="tab7"><a href="#tab-7" class="tab">Geek</a></li>
	<li id="tab8"><a href="#tab-4" class="tab">Latest</a></li>
	<li id="tab9"><a href="#tab-9" class="tab">All</a></li>

</ul>
Where the comment is in that previous javascript " // Output HTML here "

I just don't know how to properly format everything.
JasonBeee is offline   Reply With Quote
Old 10-09-2011, 12:47 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I am utterly lost.

Why would you want to use JavaScript to write that? If that code needs to be on the page EVERY TIME the page appears, why would you not just drop it into the HTML? Why is JavaScript involved?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 10-09-2011, 09:07 AM   PM User | #7
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,461
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
when you build a single-page app, all the content of all the tabs is stored on the same page, so the hash changes don't load anything from php, so there's nothing to insert upon click either.

typically, a click on a tab link hides all open tabs, and shows the tab named in the link. in order for the tab setup to be bookmarked and history'd, use a simple onload event to find location.hash, and .click() the appropriate tab link to show the tab content specified by the url. you can also likely just raise the onhashchange() event, and let the normal handlers display the correct content.

you can also use the :target css pseudo-selector to match hashes with tab content, so that folks w/o js can still navigate the page and open/close tabs as expected (ex: div:target {display:block;})

@old penant: you want to do this so you don't have to use ajax and/or so interaction widgets are bookmark-able.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%

Last edited by rnd me; 10-09-2011 at 09:10 AM..
rnd me is offline   Reply With Quote
Old 10-09-2011, 10:40 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
you want to do this so you don't have to use ajax and/or so interaction widgets are bookmark-able
Do what? Create the HTML via JavaScript? Or use the tags in this way. I understand using the tags. I see no reason to create the HTML via JavaScript.

Not to mention that it's not search-engine friendly.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 01:48 AM.


Advertisement
Log in to turn off these ads.