ProAnswers.org

Method for Transfer data with Bookmarklet?

I am trying a bookmarklet for a service. I need to transfer data (url, text) from open window but I don’t know which would be the best method.

If data is in large size then You could use POST. Create a hidden iframe with a form with a textbox. Set the form method to post and the action to your service. Put the data into the textbox, attach the iframe to the document, and submit the form.

Try this:




	
		
			function postData (data, url, cb) { 
			 
			    var f     = document.createElement('iframe'), 
			        fname = (+((''+Math.random()).substring(2))).toString(36); 
			 
			    f.setAttribute('name', fname); 
			    f.setAttribute('id', fname); 
			    f.setAttribute('style', 'width:0;height:0;border:none;margin:none;padding:none;position:absolute;'); 
			 
			    document.body.appendChild(f); 
			 
			    var frame = window.frames[fname],  
			        doc   = frame.document, 
			        form  = doc.createElement('form'), 
			        text  = doc.createElement('textarea'); 
			 
			    text.setAttribute('name', 'data'); 
			    text.appendChild(doc.createTextNode(data)); 
			 
			    form.setAttribute('action', url); 
			    form.setAttribute('method', 'post'); 
			    form.appendChild(text); 
			 
			    doc.body.appendChild(form); 
			 
			    if (cb) { document.getElementById(fname).onload=cb; } 
			 
			    doc.forms[0].submit(); 
			  }