markisgod Posted July 29, 2008 Share Posted July 29, 2008 Hello, I know that with inline javascript, you can put code like this: java script:void(document.write('<form><input></form>')) And that simply makes a form input box on the webpage (after you put it in the URL bar and hit enter).What I want to do is make a bookmark with javascript code in it, so when I click on it, a prompt box will pop up, then I enter in whatever and it stores the value I provided to a variable.However, java script:void(document.write('var name=prompt("Enter Name","");')) Does not work. I tried putting in <script type="text/javascript"> and things like that, but to no avail.Any suggestions? Link to comment Share on other sites More sharing options...
justsomeguy Posted July 29, 2008 Share Posted July 29, 2008 Why are you using Javascript to write Javascript code to the page to get executed, when you could just execute the code?java script:var name=prompt("Enter Name",""); Link to comment Share on other sites More sharing options...
Ingolme Posted July 29, 2008 Share Posted July 29, 2008 Why do you need to do that? I've never seen void() used like that before. document.write() will only work if there's actually a document to write in. A blank window doesn't have a document.If you use document.write() after a page has loaded, it will erase everything else from the page without writing. Link to comment Share on other sites More sharing options...
markisgod Posted July 29, 2008 Author Share Posted July 29, 2008 Why do you need to do that? I've never seen void() used like that before. document.write() will only work if there's actually a document to write in. A blank window doesn't have a document.If you use document.write() after a page has loaded, it will erase everything else from the page without writing.That's exactly what I want to do.I want to put this inline javascript as the URL for a bookmark, so when I open the bookmark, a prompt box pops up and I type in 'Eggs' or soemthing and it brings me to the google definiton or wikipedia page of 'eggs'.I want to erase everything on the page, replacing it with my own code. However, I cannot figure out how to put javascript into the page using javascript. Link to comment Share on other sites More sharing options...
Ingolme Posted July 29, 2008 Share Posted July 29, 2008 This should do it:javascript: var P = prompt("Search in Wikipedia:"); window.location = "http://en.wikipedia.org/wiki/"+P;void(0); Link to comment Share on other sites More sharing options...
markisgod Posted July 29, 2008 Author Share Posted July 29, 2008 This should do it:javascript: var P = prompt("Search in Wikipedia:"); window.location = "http://en.wikipedia.org/wiki/"+P;void(0); Hm, that doesn't appear to work. The webbrowser DOES navigate to Wikipedia, but only to NULL.It didn't work either way I put it - java script: var P = prompt("Search in Wikipedia:"); window.location = "http://en.wikipedia.org/wiki/"+P;void(0); -or - java script:void(document.write('java script: var P = prompt("Search in Wikipedia:"); window.location = "http://en.wikipedia.org/wiki/"+P;void(0);') Link to comment Share on other sites More sharing options...
Ingolme Posted July 29, 2008 Share Posted July 29, 2008 It's working for me in Firefox, I didn't try in Internet Explorer. What browser are you using?Apparently, Internet Explorer runs the window.location line before the prompt box is changed. I don't know if Internet Explorer can run a function like this. Link to comment Share on other sites More sharing options...
markisgod Posted July 29, 2008 Author Share Posted July 29, 2008 Ah! It does work in Firefox!Thank you, for the clever/quick reply! Link to comment Share on other sites More sharing options...
justsomeguy Posted July 29, 2008 Share Posted July 29, 2008 It might be more useful to wrap this in a function and then set the function as the code to run. function wikisearch(){ var val = prompt("Enter a search term"); window.location.href = "http://www.wikipedia.org/" + escape(val);} <a href="java script:void(0);" onclick="wikisearch()">search</a> Link to comment Share on other sites More sharing options...
Ingolme Posted July 29, 2008 Share Posted July 29, 2008 OK, this should work in Internet Explorer, but unfortunately its popup blocker gets in the way, which seems to cause a stack overflow error. After you've allowed popups from the page it will let you use this correctly.Internet Explorer has so many problems...java script: var P = prompt("Search in Wikipedia:"); function wiki() { if(!P) { setTimeout(wiki(P),500);} else { window.location = "http://en.wikipedia.org/wiki/" + P } }; wiki(P) Link to comment Share on other sites More sharing options...
jeffman Posted July 29, 2008 Share Posted July 29, 2008 Why a prompt at all? Why not a text input next to a search button that triggers the function? You know, like a million other search boxes on the web?As a user, I despise prompts. Link to comment Share on other sites More sharing options...
Ingolme Posted July 29, 2008 Share Posted July 29, 2008 Why a prompt at all? Why not a text input next to a search button that triggers the function? You know, like a million other search boxes on the web?As a user, I despise prompts.From what I understood, he's saying he wanted a handy url he could keep in his bookmarks to just show a prompt box that would send him to Wikipedia or somewhere. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 29, 2008 Share Posted July 29, 2008 He should just use Opera. I go to the address bar and type "w WTF" to search for "WTF" on wikipedia. If I use "p" instead of "w" it searches the function reference at php.net. You can define whatever you want for any letter. I built a system once that would let me keep all of my bookmarks on my web server where I just pull up the page and I can add, remove, edit, whatever. I used it for about a week. Now if I want to search wikipedia I hold down my mouse button, move the mouse down a little bit, type "w", space, and what I'm searching for. So 2 extra key presses and a mouse movement. Better then loading a bookmark or typing a URL in order to get to another URL. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.