开发者

html inside Javascript

How to input the parameter to action tag using a variable? Example:

var actionurl="some url"
windowhandle.document.write('<form name=sample action=actionurl method="post开发者_开发知识库" </form>');
windowhandle.document.sample.submit();

This is not working. I am getting 404 Page not found error. Please give me an alternate method


You are not setting the value of actionurl in your form. Try this:

windowhandle.document.write('<form name=sample action=' + actionurl + 'method="post"> </form>');

Edit: You are also missing a ">" which I have now added.


Try this :

var actionurl="some url"
windowhandle.document.write('<form name=sample action='+ actionurl +' method="post" </form>');
windowhandle.document.sample.submit();


You could use Python's .format() function:

String.prototype.format = function() {
    var str = this;
    var i = 0;
    var len = arguments.length;
    var matches = str.match(/{}/g);
    if( !matches || matches.length !== len ) {
        throw "wrong number of arguments";
    }
    while( i < len ) {
        str = str.replace(/{}/, arguments[i] );
        i++;
    }
    return str;
};

Just paste that code into your script and it'll let you use the function.

Python's .format() is useful for clean string manipulations:

'I {} a {}'.format('am', 'string')
// I am a string

It just replaces the bracketed substrings with the parameters of the function.

Now, your new code would be like this:

var actionurl="some url"
windowhandle.document.write('<form name="sample" action="{}" method="post"></form>'.format(actionurl));
windowhandle.document.sample.submit();

Be sure to use quotes around the attributes!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜