How to pass the end of the URL to a Value=“” in a

2019-08-17 19:14发布

I have a .htlm file on my hard drive is a list of my books and at the top there is a form and a script that allows me to search the content of the file. I like to access this file while I am browsing. I use Firefox with the extension "Advanced URL Builder" this extension allows me to highlight a few words and then using the context menu select Find with...\Google or any other site I have programmed in. One of these is my own file, this opens the cursor focus on the search box but no text will appear.

The URL when I open the page looks like this:

file:///F:/MyBooks.htm?search_txt=War and Peace

the search_text after the ? is what I have programmed in "Advanced URL Builder" the portion after the = sign is the highlighted text from the previous site or page

I am using this form and script that I picked up from a website:

<form name="search" action="file:///F:/MyBooks.htm" method="post"

onSubmit="if(this.t1.value!=null && this.t1.value!='')
findString(this.t1.value);return false">

<input type="text" name="t1" id="search_txt" size=100 value="" /> 
<input type="submit" value="Find" name=b1>
</form>

<script language="JavaScript">
<!--
var TRange=null

function findString (str) {
 if (parseInt(navigator.appVersion)<4) return;
 var strFound;
 if (window.find) {

  // CODE FOR BROWSERS THAT SUPPORT window.find

  strFound=self.find(str);
  if (strFound && self.getSelection && !self.getSelection().anchorNode) {
   strFound=self.find(str)
  }
  if (!strFound) {
   strFound=self.find(str,0,1)
   while (self.find(str,0,1)) continue
  }
 }
 else if (navigator.appName.indexOf("Microsoft")!=-1) {

  // EXPLORER-SPECIFIC CODE

  if (TRange!=null) {
   TRange.collapse(false)
   strFound=TRange.findText(str)
   if (strFound) TRange.select()
  }
  if (TRange==null || strFound==0) {
   TRange=self.document.body.createTextRange()
   strFound=TRange.findText(str)
   if (strFound) TRange.select()
  }
 }
 else if (navigator.appName=="Opera") {
  alert ("Opera browsers not supported, sorry...")
  return;
 }
 if (!strFound) alert ("String '"+str+"' not found!")
 return;
}
//-->
</script>

What I need is to be able to pass the last portion of the URL to the 4th line of the form as a variable

<input type="text" name="t1" id="search_txt" size=100 value="War and Peace" />

Can you please help?

Thanks.

2条回答
Evening l夕情丶
2楼-- · 2019-08-17 19:44

You can use the Javascript function described here to parse a value from the URL:

function getParameterValue(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

You'd include that function in your script, then you can use an onload Javascript call to set the value as follows:

document.getElementById("search_text").value = getParameterValue("search_text");

For kicks, here's a full HTML page that sets the search_text parameter to a text field value:

<html>
<head>
<script>
    function getParameterValue(name) {
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( window.location.href );
        if( results == null )
            return "";
        else
            return results[1];
    }

    function setSearchText() {
        document.getElementById("search_text").value = getParameterValue("search_text");
    }
</script>
</head>
<body onload="setSearchText();">
    <input type="text" name="t1" id="search_text" size=100 value="" />
</body>
</html>
查看更多
做个烂人
3楼-- · 2019-08-17 20:04

I solved the problem of the %20 by modifying the above script only 1 line - My thanks to Kaleb and all the others that I used portion of their codes to get the job done.

<script>
function getParameterValue(name) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( unescape(window.location.href) );
    if( results == null )
        return "";
    else
        return results[1];
}

function setSearchText() {
        document.getElementById("search_text").value = getParameterValue("search_text");
}

var results = regex.exec( unescape(window.location.href) ); by changing this line with the word "unescape" now the text passes into the search box without the %20.

查看更多
登录 后发表回答