Javascript打开一个新窗口并更改标题文本

时间:2016-03-14 14:13:15

标签: javascript jquery html

表格上有四个按钮。每当点击其中一个按钮时,我希望javascript重定向到新页面,并根据点击的按钮更改标题。我尝试使用window.document.write来替换标题文本但是没有用。这是我的代码:

$('#claimant_search_button', '#ca_search_button', '#emp_search_button',
            '#ea_search_button')
    {

        if (this.id == 'claimant_add_button') {

            window.location("https://dol-oalj-dev.entellitrak.com/etk-dol-oalj-dev/tracking.base.create.request.do?dataObjectKey=object.maintenanceForm&button=claimant");

        } else if (this.id == 'ca_add_button') {

            window.location("https://dol-oalj-dev.entellitrak.com/etk-dol-oalj-dev/tracking.base.create.request.do?dataObjectKey=object.maintenanceForm&button=ca");

        } else if (this.id == 'emp_add_button') {

            window.location("https://dol-oalj-dev.entellitrak.com/etk-dol-oalj-dev/tracking.base.create.request.do?dataObjectKey=object.maintenanceForm&button=emp");

        } else if (this.id == 'ea_add_button') {

            window.location("https://dol-oalj-dev.entellitrak.com/etk-dol-oalj-dev/tracking.base.create.request.do?dataObjectKey=object.maintenanceForm&button=ea");

        }

    }

以下是我在重定向页面上的标题:

<h1 id ="title_form "style="margin-bottom: 25px;"> Maintenance Form </h1>

如果用户点击claimant_search_button。我希望页面重定向到:

Redirect Link

然后更改标题以说明&#34;索赔人维护表格&#34;

1 个答案:

答案 0 :(得分:1)

我注意到URL在每种情况下都是相同的,我知道您希望浏览器完全重定向到该URL。因此,在我看来,最干净的解决方案是在您的URL中添加一个查询参数,其中包含所单击按钮的标识符。例如:

<a id="claimant_search_button" href="https://dol-oalj-dev.entellitrak.com/etk-dol-oalj-dev/tracking.base.create.request.do?dataObjectKey=object.maintenanceForm&button=claimant">Button text</a>
<!-- Other buttons here -->

请注意添加到网址的 button = claimant 。这样,您就不需要Javascript了。但是,您需要它来获取URL中的按钮查询参数并相应地设置标题。

编辑:在目标网页

var button_name = getParameterByName('button');

if(button_name=="claimant"){
    document.getElementById("title_form").innerText = "Claimant Maintenance Form";
}

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    url = url.toLowerCase(); // This is just to avoid case sensitiveness  
    name = name.replace(/[\[\]]/g, "\\$&").toLowerCase();// This is just to avoid case sensitiveness for query parameter name
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

Source