使用JavaScript替换某些文本部分 [英] Replacing Certain Text Parts Using JavaScript

查看:106
本文介绍了使用JavaScript替换某些文本部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JavaScript将以下网址分配给网页中的变量。代码片段如下。



 googleURL = '  https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions=' + '  GA%3Asource&安培;度量= GA%3Ausers&安培;排序= -ga%3Ausers&安培;开始日期= 2014年4月5日&安培;结束日期= 2014年4月23日&安培;最大结果= 10 '; 





startdate& enddate值是静态的。现在我希望那些相关的日期是动态的&日期将来自HTML表格&我已经完成了。这些值已经分配到2个不同的JS变量中。但我将这些变量添加到URL如下。



 googleURL = '  https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions='  + '  ga%3Asource& metrics = ga%3Ausers& sort = -ga%3Ausers& start-date = strStartDate& end-date = strEndDate& max-结果= 10'; 





请查看开始日期和结束日期,在那里你可以找到JS变量我用静态日期替换的名称。



当我改变编辑后的googleURL时,它显示如下。



https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions=ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers& ; start-date = strStartDate& end-date = strEndDate& max-results = 10



我认为变量值不分配t o或替换指定的变量。我正在使用的代码片段如下所示。



  var  strStartDate; 
var strEndDate;
var googleURL;

// 以下JS函数会将表格中的值传递给URL。 / span>
function passVariable(){

strStartDate = document .getElementById( from_date)。value;
strEndDate = document .getElementById( to_date 。)值;

alert(strStartDate);
alert(strEndDate);
// alert(googleURL);
文档 .WRITE(googleURL);

}

googleURL = ' https:// www。 googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions='<+' GA%3Asource&安培;度量= GA%3Ausers&安培;排序= -ga%3Ausers&安培;开始日期= strStartDate&安培;结束日期= strEndDate&安培;最大结果= 10' ;

passVariable();





我正在使用JavaScript。有人可以帮忙解决这个问题吗?



谢谢&问候,

Chiranthaka

解决方案

你可以这样做



< pre lang =cs> var strStartDate;
var strEndDate;
var googleURL;

// 以下JS函数会将表格中的值传递给URL。 / span>
函数passVariable(){

strStartDate = document.getElementById( from_date)。 value ;
strEndDate = document.getElementById( to_date)。;

alert(strStartDate);
alert(strEndDate);

googleURL = ' https://www.googleapis.com/analytics/v3/data / ga?ids = ga%3A76546294& dimensions =' + ' ga%3Asource& metrics = ga% 3个用户& sort = -ga%3Ausers& start-date =' + strStartDate + ' & end- date =' + strEndDate + ' & max-results = 10';

document.write(googleURL);

}
passVariable();


I have the following URL assigned to a variable in a webpage using JavaScript. The code snippet is below.

googleURL = 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions='+'ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-date=2014-04-05&end-date=2014-04-23&max-results=10';



The startdate & enddate values are static. Now I want that perticualr dates to be dynamic & the dates will be get from a HTML form & I already have that done. Those values are assigned into 2 different JS variables already. But I add those variables to the URL as below.

googleURL = 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions='+'ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-date=strStartDate&end-date=strEndDate&max-results=10';



Please look at start-date and end-date, there you can find the JS variable names that I have replaced with static dates.

When I alterted the edited googleURL, it displays as at the below.

https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions=ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-date=strStartDate&end-date=strEndDate&max-results=10

I think the variable values are not assigning to or replacing the specified variables. The code snippet I'm working is at the below.

var strStartDate;
var strEndDate;
var googleURL;

//The following JS function will pass the values from the form to the URL.
function passVariable() {

  strStartDate = document.getElementById("from_date").value;
  strEndDate = document.getElementById("to_date").value;

  alert(strStartDate);
  alert(strEndDate);
  //alert(googleURL);
  document.write(googleURL);

}

 googleURL = 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions='+'ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-date=strStartDate&end-date=strEndDate&max-results=10';

 passVariable();



I'm using JavaScript. could someone helpme to solve this matter?

Thanks & regards,
Chiranthaka

解决方案

you can done like this

var strStartDate;
var strEndDate;
var googleURL;

//The following JS function will pass the values from the form to the URL.
function passVariable() {

  strStartDate = document.getElementById("from_date").value;
  strEndDate = document.getElementById("to_date").value;

  alert(strStartDate);
  alert(strEndDate);

googleURL = 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions='+'ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-date=' + strStartDate + '&end-date=' + strEndDate + '&max-results=10';

  document.write(googleURL);

}
 passVariable();


这篇关于使用JavaScript替换某些文本部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆