请问如何获取当前的html页面标题并传递给提交表单

时间:2022-05-15 01:22:54

Pls i would want the script to be in javascript only. I would want to get the page title and pass it to a submit form, so when its being submited, i can see the actual page name user sent the request from.

请问我希望脚本只能在javascript中使用。我想获取页面标题并将其传递给提交表单,因此当它被提交时,我可以看到用户发送请求的实际页面名称。

1 个解决方案

#1


try this Using Jquery

试试这个使用Jquery

$(document).find("title").text();

Or in generic way

或者以通用的方式

var title = document.getElementsByTagName("title")[0].innerHTML;

Update

you can append item in form like this

你可以在这样的形式下追加项目

function AppendHiddenField(theForm) {
    // Create a hidden input element, and append it to the form:
    var input = document.createElement('input');
    input.type = 'hidden';
    input.name ='PageTitle';
    var Pagetitle = document.getElementsByTagName("title")[0].innerHTML;
    input.value = Pagetitle;
    theForm.appendChild(input);
}

you can use this function to append a hidden field to the form before submitting it.

您可以使用此函数在提交表单之前将隐藏字段附加到表单。

Update Here is working code you need to assign your form an id.

更新此处是您需要为表单分配ID的工作代码。

JS

window.onload = function(){
var input =document.createElement('input');
var frm = document.getElementById("myform");
input.type = 'hidden';
input.name ='PageTitle';
input.value = document.title;
frm.appendChild(input);
}

HTML

<form id="myform" method="submit" action="http://www.facebook.com/sharer/sharer.php?u=http://lmn.9nty.com/?testlink=www.learnmoreng.com/site_rp.xhtml"> <input type="text" name="l" value=":getid-url:"
maxlength="20" size="9"/>
<input type="submit" value="Share" style="background-color:#f3f4f5;padding:3;color:#339900;font-weight:italic"/></form> 

#1


try this Using Jquery

试试这个使用Jquery

$(document).find("title").text();

Or in generic way

或者以通用的方式

var title = document.getElementsByTagName("title")[0].innerHTML;

Update

you can append item in form like this

你可以在这样的形式下追加项目

function AppendHiddenField(theForm) {
    // Create a hidden input element, and append it to the form:
    var input = document.createElement('input');
    input.type = 'hidden';
    input.name ='PageTitle';
    var Pagetitle = document.getElementsByTagName("title")[0].innerHTML;
    input.value = Pagetitle;
    theForm.appendChild(input);
}

you can use this function to append a hidden field to the form before submitting it.

您可以使用此函数在提交表单之前将隐藏字段附加到表单。

Update Here is working code you need to assign your form an id.

更新此处是您需要为表单分配ID的工作代码。

JS

window.onload = function(){
var input =document.createElement('input');
var frm = document.getElementById("myform");
input.type = 'hidden';
input.name ='PageTitle';
input.value = document.title;
frm.appendChild(input);
}

HTML

<form id="myform" method="submit" action="http://www.facebook.com/sharer/sharer.php?u=http://lmn.9nty.com/?testlink=www.learnmoreng.com/site_rp.xhtml"> <input type="text" name="l" value=":getid-url:"
maxlength="20" size="9"/>
<input type="submit" value="Share" style="background-color:#f3f4f5;padding:3;color:#339900;font-weight:italic"/></form>