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

1 Answer 1

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; } 
Sign up to request clarification or add additional context in comments.

4 Comments

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 )
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?
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 .
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.