0

I want to add some data from url to an input-box with an unique name ( not id ) , Because I don't have any accesses on the page I can't edit it with ids or sth.

<input type="text" name="test">

and sth like that :

site.com/index.php?test=text123

user1599537
  • 1,269
  • 3
  • 10
  • 10

1 Answers1

1

Ok from what I understand, You want to put get data from URL into your input fields.

First, when you open the tab, put the 'document' of the tab into a global variable

var url = "www.url.com"; //the url of the page to open in tab
var tabInstance= window.open(url);

tabDocument =  tabInstance.document; //tabDocument is a global variable

Now, assuming the data you want to put into the tab is in the URL of the page that is opening the tab

function populateInputFields(){
    var data = parseURLParams(document.URL); //get url data in json format.
    if(!data) return; //if no get parameters found
    //iterate json
    for(var key in data){//for each key in the json data
        var value = data[key]; //get the 'value' for corresponding key 
        var element = tabDocument.getElementsByTagName(key)[0];//get the input element 
        if(element && element.tagName == 'input'){//check if element exists and is of type input
            element.value = value;
        }

    }
}

Implementation of parseURLParams take from here: How to read GET data from a URL using JavaScript?

function parseURLParams(url) {
    var queryStart = url.indexOf("?") + 1,
        queryEnd   = url.indexOf("#") + 1 || url.length + 1,
        query = url.slice(queryStart, queryEnd - 1),
        pairs = query.replace(/\+/g, " ").split("&"),
        parms = {}, i, n, v, nv;

    if (query === url || query === "") {
        return;
    }

    for (i = 0; i < pairs.length; i++) {
        nv = pairs[i].split("=");
        n = decodeURIComponent(nv[0]);
        v = decodeURIComponent(nv[1]);

        if (!parms.hasOwnProperty(n)) {
            parms[n] = [];
        }

        parms[n].push(nv.length === 2 ? v : null);
    }
    return parms;
}
Community
  • 1
  • 1
Ahmed-Anas
  • 5,471
  • 9
  • 50
  • 72
  • I don't have any accesses on the input pages I want to do this just by editing a sender page in which I have the link ( site.com/index.php?test=text123 ) – user1599537 Sep 04 '14 at 07:24
  • 1
    Oh.. Are the input pages opened in a popup, an iframe, or something like that? Or is there some javascript or other mechanism available in the input page? – Ahmed-Anas Sep 04 '14 at 07:26
  • yes , it opens in a new tab . there no JavaScript or Seth else available in the page and I don't have any control over that page , it just has a input box with a name. ty for your response . – user1599537 Sep 06 '14 at 09:10
  • Updated my answer. The key is to get the document of the tab you open using "documentTab = window.open('www.site.com/foo')".document .This is ONLY possible if the url of the tab being opened and the url of the page opening the tab have the same url. Otherwise this is not posbible. – Ahmed-Anas Sep 08 '14 at 05:38