Jump to content

Can HTMLDOM POST pass a Javascript function as a variable string?


swirlingDervish

Recommended Posts

I'm trying to, based on a button click, redirect to another webpage within a domain and then open a div on that new page, all by Javascript. Is that possible? I've searched the forum and Stackflow and found similar but not quite, answers: most want to call the function from the present page they're on. Any thoughts?

Link to comment
Share on other sites

 

...button click, redirect to another webpage within a domain and then open a div...

 

All files on the same domain? What is "open a div?"

 

Javascript can't read a POST, but it can decode a GET, and it can read local storage on the same domain.

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

Link to comment
Share on other sites

So, Ingolme, I wouldn't have to use a form structure, right?, to actually send the parameters through the query string, just a variable declaration...?

 

And davej, all files are on the same domain. "Open a div", I meant return a div's innerHTML through JS. JS can decode a GET but not a POST? Is that because of POST's header info?

Link to comment
Share on other sites

I still don't understand what you are doing. It doesn't matter what you do with the data once it is received. You can put it in a div or do whatever you want with it.

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title>URLreader</title><style></style><script>window.onerror = function(a, b, c, d){alert('Javascript Error: '+a+'nURL: '+b+'nLine: '+c+'  Column: '+d);return true;}</script><script>'use strict';window.onload = init;function init() {var str = 'location.search   = '+ location.search +'<hr/>';var txt = location.search;if (txt.indexOf('?')==0){txt = txt.substr(1);// trim leading '?'var txtsplit = txt.split('&');for (var i=0,len=txtsplit.length ; i<len ; i++){  var ss = txtsplit[i].split('=');  var ss1s = ss[1].replace(/+/g,' ');  str += '<br/>'+ ss[0] + ' = ['+ decodeURIComponent(ss1s) +']';}}document.getElementById("out").innerHTML = str;}</script></head><body><div id="out"></div></body>    </html>
Link to comment
Share on other sites

Great parser, davej, but I'm not just trying to parse a string for the div, I'm trying to parse a function sent as a string passed by POST to be executed on the div of the new page. Foxy Mod is saying I can't...

 

Maybe if I include some pages: www.siddhicenter.org and www.siddhicenter.org/Inner/Space/Descriptions.htm. The buttons on the home page "Mantra Yoga" and "Guru Yoga", when clicked, should open the Descriptions.htm page and on loading, open the script already on that page to open the passed description number that corresponds with the above Yogas. Hope that might be a little clearer, thanks.... :umnik2:

Link to comment
Share on other sites

Are you asking if you can just put Javascript code in the query string of a URL?

http://www.example.com?function(param1, param2) { alert(param1 + " " + param2); }

You can put anything in a query string if you encode it (substituting spaces for %20 and other invalid characters with their character codes). You can have Javascript read the query string and execute code using eval().

 

It's possible, but it's really really bad.

 

 

 

You need to get to the underlying reason you want a system like this. Let's pretend you don't know anything about programming, you're just a user that's visiting a website. What do you want to see on the website? Show an example of another website that does what you want.

Link to comment
Share on other sites

I figured it might be messy. I guess I want to save the work of having to throw a stylesheet on every page with more client-side heavy code and re-position all my elements on each page to accommodate a couple of descriptions that already have a dedicated webpage. The two description buttons are on each page of the website, so it just seems easier to go directly to the dedicated page, then call up the description. IOW, I want the description nav buttons to open the description, then open the description it is. Am I going at this the wrong way?

 

www.siddhicenter.org - home

www.siddhicenter.org/Inner/Space/Descriptions.htm - definition-descriptions

Link to comment
Share on other sites

On your description page you have a function that loads a description. Let's call it loadDescription(id)

That function should put content in the description box based on what value you passed to it.

 

You can add a hash to the URL in a link:

www.siddhicenter.org/Inner/Space/Descriptions.htm#something

 

When the page loads, you can tell Javascript to read the value of the hash and if it has the right value then you can call loadDescription()

This example uses switch().

switch(location.hash) {case "#something":    loadDescription(0);    break;case "#something-else":    loadDescription(1);    break;}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...