简体   繁体   中英

Passing parameter from URL in Google app script

I have a problem with passing parameter from url "?id=5" in HTML5 page inside Google site.

I designed a page in Google site, and I inserted my app script inside the site as iframe, but the parameter not passed although I tested the code in ordinary HTML page, it works. What can I do?

The JavaScript code is :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript">


           var param1var = getQueryVariable("id");

           var param1var2 = getQueryVariable("id2");


             alert(param1var + param1var2);


    function getQueryVariable(variable) {

     var query = window.location.search. substring(1);
     var vars = query.split("&");
     for (var i = 0; i < vars.length;i++) {

         var pair = vars[i].split("=");

         if (pair[0] == variable) {

             return pair[1];       

         }  

     }


 alert('Query Variable ' + variable + ' not found');

       }

There is a small typo error (an extra space) on this line:

var query = window.location.search. substring(1);

Also your script tag doesn't have a closing tag.

Further: parsing url parameters is a very common javascript task. Jquery has built-in functionality for it, or if you want to use plain JS, checkout this thread: How can I get query string values in JavaScript?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM