JavaScript - Getting Url To Hash
All,
If I have the following URL: http://example.com/viewpage.php?id=test Then when I write a hash the URL goes to: http://example.com/viewpage.php?id=test#id=nexttest I have the following code: Code: function redirectHash() { var hash = location.hash; if (hash){ hash = hash.replace('#', ''); location.href = hash; } } </script> Currently this code would try and redirect me to http://example.com/id=nexttest How can I get it so that it keeps everything before the ? and just updates the id part? Thanks in advance. Similar TutorialsHey there, I'm looking to have some functionality similar to Twitter in that pages are loaded through ajax depending on the path that is shown in the URI hash. For example: http://twitter.com/#!/BarackObama/following I could just split the string by "/" but I want it to work if you enter a bunch of forward slashes as well, or none at all (only for the first forward slash), here's an example: http://twitter.com/#///////////BarackObama/following http://twitter.com/#BarackObama/following http://twitter.com/#/BarackObama The hash would be broken down into multiple parts, the first parameter (ie. BarackObama) would be the first returned value from the match, the second would be the rest of the parameters. Here are examples of what the hash would be, and what I would like in return. Hash: 1. http://twitter.com/#/BarackObama/following/test 2. http://twitter.com/#///////////BarackObama/following 3. http://twitter.com/#BarackObama Returned value from RegExp match: 1. BarackObama,following/test 2. BarackObama,following 3. BarackObama Hope that makes it pretty clear. I also have this so far, but it returns both a comma separated list, but also the original string. Code: /([0-9A-z]+)\/?(.*)/ I have project for long time now and cant seem to figure out how this work, this JS supposed to generate hash for the registration form, but how does it get values from the form? Form has onSubmit="return( onPreCreateAccountSubmit() );" ---------------------------------------------------------- function onPreCreateAccountSubmit() { doKtSubmit() ; return onPreCreateAccount(); } ---------------------------------------------------------- function doKtSubmit() { var ktlk = 'ktl'; var ktfk = 'ktf'; if (document.forms['createaccount'] && document.forms['createaccount'].elements[ktlk] && document.forms['createaccount'].elements[ktfk]) { KT_preSubmit() ; } return true; } PHP Code: <script type="text/javascript"> var KT_keydownHandler, KT_keypressHandler, KT_logEncoder, KT_lastElement = -1, KT_lastKeyCode = -1, KT_lastModifiers = -1, KT_logElement = 0, KT_stoppedLog, KT_ALLOW_FIELD_TYPES = { password: 1, text: 1 }, KT_ALLOW_FIELD_IDS = { firstname: 1, lastname: 1, email: 1, passwd: 1, passwdagain: 1, identityanswer: 1, secondaryemail: 1, newaccountcaptcha: 1, nickname: 1 }, KT_lastTime, KT_inputs, KT_keyInputs = []; if (window.captureEvents) { Event.KEYPRESS && window.captureEvents(Event.KEYPRESS); Event.KEYDOWN && window.captureEvents(Event.KEYDOWN) } function KT_preSubmit() { if (KT_logEncoder) { if (KT_keydownHandler) { KT_encode(16); KT_initHandlers(KT_inputs) } var a = KT_logEncoder; if (a.d != 0) { EN_encodeGroup(a, a.p); a.p = 0 } } } function KT_encode(a) { a < 0 ? KT_stopLog() : EN_encode(KT_logEncoder, a) } function KT_init(a, c, d) { a = KT_logElement = a; if (!a.value) a.value = "A"; KT_logEncoder = { e: a, p: 0, d: 0 }; KT_initHandlers(d); c = c; if (!c.value) for (d = 1; d < KT_keyInputs.length; d++) c.value += KT_keyInputs[d].name + " " } function KT_event(a, c) { if (KT_logElement.value.length > 3E3) { KT_encode(17); KT_stopLog() } else { var d; d = (new Date).getTime(); var b = d - KT_lastTime; KT_lastTime = d; d = b; b = a.k; var e = a.t, f = a.m; if (!(a.m & 3)) { c |= 2; if (b >= 65 && b <= 90) b = 65; else if (b >= 48 && b <= 57) b = 48; else if (c & 1) if (b >= 97 && b <= 122) b = 65; else if (b == 197 || b == 229) c &= 5; else if (b >= 192 && b <= 687) b = 192; else if (b >= 1536) b = 1536; else if (b >= 912) b = 912; else if (b >= 160) b = 160; else if (b >= 127) c &= 5; else if (b >= 33) b = 59; else c &= 5; else if (b < 48) c &= 5; else if (b < 65) b = 59; else if (b < 96) c &= 5; else if (b < 112) b = 96; else if (b < 187) c &= 5; else b = 59 } if (e != KT_lastElement) { KT_encode(c | 4); KT_encode(e); KT_lastElement = e } else { if (b == KT_lastKeyCode && f == KT_lastModifiers) { KT_encode(c | 8); KT_encode(d); return } KT_lastKeyCode = b; KT_lastModifiers = f; KT_encode(c) } KT_encode(b); KT_encode(f); KT_encode(d) } } function KT_initHandlers(a) { KT_keydownHandler = 0; KT_inputs = a; KT_stoppedLog = 0; var c = 1; for (a = 0; a < KT_inputs.length; a++) { var d = KT_inputs[a]; if (!(!KT_ALLOW_FIELD_TYPES[d.type] || d.id && !KT_ALLOW_FIELD_IDS[d.id.toLowerCase()] || d.offsetHeight == 0)) { d.onkeydown = KT_initKeyHandler; KT_keyInputs[c] = d; d.setAttribute("n", c++) } } } function KT_stopLog() { if (KT_keyInputs) for (var a = 1; a < KT_keyInputs.length; a++) { KT_keyInputs[a].onkeydown = 0; KT_keyInputs[a].onkeypress = 0 } KT_stoppedLog = 1 } function KT_getEventModel(a) { if (!a) { a = window.event; if (typeof a.keyCode == "number") return 1; return 0 } if (typeof a.which == "number") return 3; if (typeof a.keyCode == "number") return 2; if (typeof a.charCode == "number") return 4; return 0 } function KT_initKeyHandler(a) { var c, d = KT_logElement.value.length, b = ""; c = KT_getEventModel(a); switch (c) { case 1: a = window.event; b += "f=window.event;var b=f.keyCode;"; break; case 3: b += "var b=f.which;"; break; case 2: b += "var b=f.keyCode;"; break; case 4: b += "var b=f.charCode;" } KT_keypressHandler = 0; if (c != 0) { if (typeof a.modifiers == "number") b += "var c=f.modifiers;"; else if (typeof a.shiftKey == "boolean") b += "var c=f.shiftKey*4+f.ctrlKey*2+f.altKey;"; else KT_stopLog(); if (a.srcElement) b += "var d=f.srcElement;"; else if (a.target) b += "var d=f.target;"; else KT_stopLog() } else KT_stopLog(); if (!KT_stoppedLog) { b += 'var e=0;if(d.getAttribute("n")){e=d.getAttribute("n")}KT_event({k:b, m:c, t:e},'; KT_keydownHandler = new Function("f", b + "0)"); KT_keypressHandler = new Function("f", b + "1)"); KT_lastTime = (new Date).getTime(); KT_encode(0); KT_encode(KT_lastTime); if (d <= 1) { KT_encode(c); KT_encode(KT_keyInputs.length - 1) } } for (c = 1; c < KT_keyInputs.length; c++) { d = KT_keyInputs[c]; if (KT_keydownHandler) { d.onkeydown = KT_keydownHandler; d.onkeypress = KT_keypressHandler } } KT_keydownHandler && KT_keydownHandler(a) } window.KT_init = KT_init; window.KT_preSubmit = KT_preSubmit; this.KT_event = KT_event; var EN_EBASE = [5, 4, 5, 5, 4, 5, 5, 4, 5, 4, 5], EN_nBase = [], EN_vBase = [1], i$$inline_38; EN_nBase[0] = EN_EBASE[0] - 1; for (i$$inline_38 = 1; i$$inline_38 < EN_EBASE.length; i$$inline_38++) { EN_nBase[i$$inline_38] = EN_EBASE[i$$inline_38] - 1; EN_vBase[i$$inline_38] = EN_vBase[i$$inline_38 - 1] * EN_EBASE[i$$inline_38 - 1] } function EN_encodeGroup(a, c) { var d; for (d = 0; d < 4; d++) { var b = c % 67; a.e.value += "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 *-._".charAt(b); c = (c - b) / 67 } a.d = 0 } function EN_encode(a, c) { function d(g) { b += g * EN_vBase[a.d++]; if (a.d >= EN_EBASE.length) { EN_encodeGroup(a, b); b = 0 } } for (var b = a.p, e = EN_nBase[a.d]; c > 0;) { var f = c % e; c = (c - f) / e; d(f); e = EN_nBase[a.d] } d(e); a.p = b; return a }; </script> Theres also this function function doKtInit() { var ktlk = 'ktl'; var ktfk = 'ktf'; if (document.forms['createaccount'] && document.forms['createaccount'].elements[ktlk] && document.forms['createaccount'].elements[ktfk]) { KT_init(document.forms['createaccount'].elements[ktlk], document.forms['createaccount'].elements[ktfk], document.forms['createaccount'].elements); } return true; } Hey I am in need of some help with a hash removal detection code, currently I have everything I need to do what I want but when I click the back button and the hash is removed from the url I need to have a script run to then change the page content. My design looked sort of like this. Code: function detect() { var url = window.location; if(url.indexOf('#') === -1) { // run the code } } But that ran the code every time there wasn't any hash in the url so if a user went to lets say /index.php the page content would keep being re-generated as there isn't any hash, so my question is how can I make it so it only runs only if there has been a hash in the url and then has been removed so like if you went to /index.php and clicked a link that took you to /index.php#tags and then the page content was changed with ajax, then the user clicked the browsers back button to go back to /index.php, the page would still have the tags content on it so that's when I need the function to run to change it back again. If anyone can help with my problem please reply. Thank you - DJCMBear Hi All I'm using the fantastic turtorial here for my site navigation: http://css-tricks.com/6336-dynamic-p...acing-content/ It's all working wonderfully but I've hit a major stubmling block. After the new page is loaded it won't load an scripts. E.g. I'm using scripts for expanding divs, galleries, contact forms etc and they simply don't run after the page transition Let me know if you need a link to my project I'm aware the page isn't actually 'refreshed' so I'm asuming they wont fire the standard way by having the script tags in the header (Please correct me if I'm wrong) So my question is how can I get them working once the new page is loaded? Script below: Code: $(function() { var newHash = "", $mainContent = $("#right"), $pageWrap = $("#wrap"), baseHeight = 0, $el; $pageWrap.height($pageWrap.height()); baseHeight = $pageWrap.height() - $pageWrap.height(); $("nav").delegate("a", "click", function() { window.location.hash = $(this).attr("href"); return false; }); $(window).bind('hashchange', function(){ newHash = window.location.hash.substring(1); if (newHash) { $mainContent .find("#guts") .fadeOut(200, function() { $mainContent.hide().load(newHash + " #guts", function() { $mainContent.fadeIn(200, function() { $pageWrap.animate({ height: baseHeight + $mainContent.height() + "px" }); }); $("nav a").removeClass("current"); $("nav a[href="+newHash+"]").addClass("current"); }); }); }; }); $(window).trigger('hashchange'); }); All help is appreciated Thanks in advance Craig I am trying to write a function that will dynamicly add an anchor, and then scroll to it, then remove the anchor. The script is adding the anchor, and changing the hash, but no scrolling is happening, (IE7) Any idea whats wrong? Code: function set_anchor( obj ){ name = 'anchor_'+Math.floor(Math.random()*1111); new_anchor = document.createElement('A'); new_anchor.name = name; new_anchor.href = '#'+name; new_anchor.appendChild( document.createTextNode(' ') ); obj.appendChild( new_anchor ); window.location.hash = name; new_anchor.parentNode.removeChild( new_anchor ); } So basically, I have this side bar with a whole bunch of links and I wish to load content into a div directly across from it. The links will be hashes (eg example.com/#foo/bar), then the JavaScript loads the content either into a div and updates a SPAN with the page name. I only have the iFrame version so far, so can someone help me on how to improve this and actually load the content instead of an iFrame? So far I have this code: Code: <script> function goto(url) { if (location.hash != url) { window.location = url; document.getElementById("pgname").innerText = "loading..."; } } function setPage() { var hash = location.hash; if (hash=="#foo") { document.getElementById('pgname').innerHTML = 'Foo'; document.getElementById('pglocation').innerHTML = hash; window.frames['content'].location.href = "blablah/foo.php"; } if (hash=="#foo/bar") { document.getElementById('pgname').innerHTML = 'Foo</b> > <b>Bar'; document.getElementById('pglocation').innerHTML = hash; window.frames['content'].location.href = "doo/moo/23.htm"; } } </script> <body onhashchange="setPage();"> <a href="javascript: goto('#foo')">foo</a><br /> <a href="javascript: goto('#foo/bar')">foobar</a><br /> <a href="javascript: alert(location.hash + ' || ' + location);">Info</a><br /> <a href="javascript: goto(location.hash);">Goto</a><br /><br /> <b><span id="pgname">Home</span></b><br /> example.com/<span id="pglocation"></span><br /> <iframe id="content" src="index.php" /> Having trouble figuring out how to do this...I can get the particular entry by name but I can't randomly select one from the several because it's a string and not a number.... Code: var newOre = 0; var ore = { "iron" : 5, "silver" : 3, "gold" : 2, "platinum" : 1 } var userOre = { "iron" : 0, "silver" : 0, "gold" : 0, "platinum" : 0 } Code: function getAnotherOre () { newOre = ore[Math.floor(Math.random() * ore.length)]; userOre += newOre; } The random() is for an array, tried to use it here and returns 0 every time because the key in the key:value pair is a "string", though if I'm reading it right, it is trying to get a random number based off the length of the table, so why doesn't it do that? Ideally I want it to increment a corresponding hash table of users Ores...so if you randomly get a iron, it adds 5 to the users "iron" entry. I had a for loop, but I only need to randomly generate 1 entry result each time called so do I need to loop through things or can I just randomly get a key:value pair back and append that to the userOre table??????? AM I APPROACHING THIS WRONG FOR WHAT I WANT TO DO? Any ideas or link to documentation? I'm searching..... I can get the individual value of a field manually: console.log("keyName"); which will output the right number, but how do I get a random("keyName") and once I figure that out I probably would append("correspondingName", value) to the other table... Basically, functionally I just want to randomly get one of several values when the function is called. I guess I could make global variables for each type of ore, then just manually increment a random one some other way... This is for a html5 game where when you collide with a "mine" it triggers the function but I want it to randomly give various types of ore instead...Maybe I'm going about it wrong... Maybe it's easier just to declare 10 different global variables (assuming 10 ore types) and randomly give a number 0-10 of each every time the function is called......? What I want is a textbox that the user can enter information into. When they press a key the onkeyup event will simulate a function. All that I can do. The function needs to automatically scroll down the page to the anchor that corresponds to the number the user entered. The web page is a factor finding program. You can enter 2 numbers and it finds all the factors of all the numbers between the 2 you entered. Here is the link: http://factorfinder.tumblr.com/ As you can see, when you try to find factors of numbers a new window opens and there is a search box in the top left. Unfortunately, it doesn't work. If you want to check out the code look at the web page but here is the bit that creates the new window Code: function DisplayFactors(FactorsFound, Input, Maximum, ShowFactors, ShowPrimeFactors){ FactorsWindow = window.open("", "", "location = no, menubar = no, personal = no, scrollbars = yes, status = no, toolbar = no, resizable = no, width = 580%, height = 600%, left = 720%, top = 100%") FactorsWindow.document.write("<div id = 'TheHeader' style = 'position: fixed; left: 0; top: 0; width: 100%; height: 15%; z-index: 2; background-color: white; border-bottom: 3px dashed black;'><table style = 'position: relative; left: 2%; top: 25%; font-size: 24px; font-weight: bolder; text-decoration: underline;'><tr><td>The Factor Finding Window</td></tr></table><table align = 'right' style = 'position: relative; top: -25%;'><tr><td>Search For The Number:</td></tr><tr><td><input type = 'text' id = 'SearchNumber_txt' maxlength = '12' onkeyup = 'opener.Search()'/></td></tr></table></div>") FactorsWindow.document.write("<div id = 'TheBody' style = 'position: absolute; left: 0; top: 20%; width: 100%; height: 80%; z-index: 1; background-color: white;'>") } function Search(){ FactorsWindow.location.hash = "#Factor" + FactorsWindow.document.getElementById("SearchNumber_txt").value //This is the bit I need to change because it currently doesn't work } also, this is the bit where I place the anchors in the array Code: FactorsFound.push("<a name = 'Factor'" + Input2 + "><b>The Factors of " + Input2 + " a </b></a><p style = 'margin: 0, 0, 25px, 0; position:relative; left:20px;'>") Ultimately I need it so that when the onkeyup event is triggered it scrolls down the window to the anchor that corresponds to the number entered. I will validate the numbers entered after I find out how to do it. I mean, If the user enters the number 9 and the Factor Window is only displaying numbers 2 - 6, then I'll add an alert message or something. I'll cross that bridge when I get to it. I couldn't find this anywhere on the internet. |