shortysbest Posted November 24, 2010 Share Posted November 24, 2010 Say i have a url like this: index.php#!/profile&id=1 how could i just select "profile" and nothing before #!/ or after &? Quote Link to comment https://forums.phpfreaks.com/topic/219656-how-could-i-slice-a-hash-url-to-select-a-certain-part-only/ Share on other sites More sharing options...
haku Posted November 24, 2010 Share Posted November 24, 2010 Give a few more examples of other possible URLs that you will be slicing, so we have a pattern to work with. It's impossible to see a pattern from only one example. Quote Link to comment https://forums.phpfreaks.com/topic/219656-how-could-i-slice-a-hash-url-to-select-a-certain-part-only/#findComment-1138862 Share on other sites More sharing options...
shortysbest Posted November 24, 2010 Author Share Posted November 24, 2010 Like I said, the url could look like: index.php#!/profile&id=1 And I would want it to return just "profile", so only returning anything between #!/ and & Another way the url could look is like: index.php#!/home And I would just want it to return "home", so anything after #!/ Quote Link to comment https://forums.phpfreaks.com/topic/219656-how-could-i-slice-a-hash-url-to-select-a-certain-part-only/#findComment-1138941 Share on other sites More sharing options...
haku Posted November 25, 2010 Share Posted November 25, 2010 As I said before, it's impossible to see a pattern from only one example. Had I given you the code with only your first example, my code wouldn't have worked for your second example. The code below will work for either of them, but if you have any other patterns, it may not work. var url = "index.php#!/profile&id=1" var start = url.indexOf("#!/") + 3; var ampersand = url.indexOf("&"); if(ampersand) { var extract = url.substr(start, ampersand); } else { var extract = url.substr(start); } The value will be contained in 'extract'. Quote Link to comment https://forums.phpfreaks.com/topic/219656-how-could-i-slice-a-hash-url-to-select-a-certain-part-only/#findComment-1139264 Share on other sites More sharing options...
shortysbest Posted November 25, 2010 Author Share Posted November 25, 2010 Thank you for your time to help me, I came up with a different way earlier that seems to work fine for what I'm doing (so far, as i'm just beginning my project). This is what I am using: var myString = document.location.hash.replace("#!/", ""); var myResult = myString.split("&"); var result_id = myResult[0]; navigation(result_id); However, that problem aside, I am having another problem to come get a solution for (as I am new to this type of javascripting). However, first of all to understand exactly what I'm trying to accomplish by the code I will show you below, I shall give a quick explanation. Okay, so what I am doing is building a completely javascript/php driven website, so basically I am never refreshing the ENTIRE page, just hitting calls for php pages with ajax. (It's a social networking kind of site). So, the problem. Well, I have the main navigation set up and working pretty good, however once I click a link in the main part of the page (the profile page), it changes the hash url, however it just doesn't load the page. Well, you have to click it twice for it to go to the new page because my function I came up with to retrieve the ID from the hashed url is in the function to change the page, It is in there because otherwise it wouldn't hit for the new url, it would just load the current one for the first time you click it, and then when you rerun the function again (by clicking the link again) it loads the new page. So it basically has a one click delay because the id identifier is inside the function(cannot be outside of it since this javascript page doesn't reload). Finally here is the code(the simplified version) function navigation(id){///id will equal "profile", this is for the main navigation, so it will load the php page below depending on which page is clicked, the main navigation part, so it could equal "home", "messages", etc. var id2 = $.getUrlVar('id'); /////this is what gets the id from the hashed url. so if the url is index.php#!/profile&id=7, var id2 will equal 7 (Basically like the php $_GET['id'] does) $.ajax({ type: "POST", url: "inc/main/"+id+".php", //////This calls the profile.php page which is what is loaded into the main part of the page, this has the sub links for other profiles and stuff,(just picture facebook profile page) data: "profile_id="+id2, "profile_id=7"(the id the javascript got from hash above); cache: false, success: function(html){ $(".container").html(html); ///Profile page is loaded into the main part of the page } }); } Quote Link to comment https://forums.phpfreaks.com/topic/219656-how-could-i-slice-a-hash-url-to-select-a-certain-part-only/#findComment-1139271 Share on other sites More sharing options...
haku Posted November 25, 2010 Share Posted November 25, 2010 What you need to do is create a function that checks the URL hash every X milliseconds, and if the hash is different from the currently loaded page, load the page in the hash. I put together an example of this a few months back here: http://ajax.jaypan.com/ This is a page that works both with and without javascript enabled. The javascript is extensively commented, so you should be able to get an idea of how it works by looking through the javascript on that page (note: it's all jQuery). Quote Link to comment https://forums.phpfreaks.com/topic/219656-how-could-i-slice-a-hash-url-to-select-a-certain-part-only/#findComment-1139292 Share on other sites More sharing options...
shortysbest Posted November 25, 2010 Author Share Posted November 25, 2010 Thank you very much for pointing me into the right direction, I used the idea of the page you showed me and made a more simplified version of it to fit what i needed it to do, and it seems to have fixed many problems i was having. Thanks alot! Quote Link to comment https://forums.phpfreaks.com/topic/219656-how-could-i-slice-a-hash-url-to-select-a-certain-part-only/#findComment-1139505 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.