开发者

add text input at end of URL

I have a simple HTML page that has a text field, a button, and a div. I want to have a user input a stock symbol into the field. When they push submit button, an image of a graph will display inside the div.

The graphs already exist @http://ichart.finance.yahoo.com/w?s=

To get a graph for a paticular symbol, I need to add the symbol, an '&', and a large random number. This is a working example

http://ichart.finance.yahoo.com/w?s=YHOO&1234567890

I am not able to make the symbol, the &, and the random number append to the end of the URL. I am also not sure if I am using form correctly.

Here is the code:

    function changeChart() {
    var rand_no = Math.random();
    rand_no = rand_no * 100000000;
    var sym = document.myform.symbol.value;
    document.getElementById('divService').innerHTML = '<' + 'object id="foo" name="foo" type="text/html" data="http://ichart.finance.yahoo.com/w?s="' + sym + '"&"' + rand_no + '"><\/object>';
}

<form name = "myform">
<p>
  Enter stock symbol
  <input id="Text1" type="text" name="symbol"/>
  <input type="button" value="Go" name="Submit" onclick="changeChart(this); return false;"/>
</p>

<div id="divService">
  <object id="foo" name="foo" type="text/html" data="http://www.aol.com/"></object>
</div>

Here is the complete code, The CSS wouldn't display properly on here. https://docs.google.com/document/d/1pq39BKxWRqDS162jWss7-fvTbr0r开发者_Go百科28wq4VFiedh8SCY/edit?hl=en


I checked just some minutes ago and it seems there's no need for &1234567890-like part of URL, so I'd change the code to get this:

function changeChart() {
  var sym = document.forms[0].elements['symbol'].value;
  var divContent = '<'+'object id="foo" name="foo" type="text/html" data="http://ichart.finance.yahoo.com/w?s=' + sym +'"/>';
  document.getElementById('divService').innerHTML = divContent;
}

Given that random number is not necessary, and in HTML code:

<form name = "myform">
<p>
  Enter stock symbol
  <input id="symbol" type="text" name="symbol"/>
  <input type="button" value="Go" name="Submit" onclick="changeChart(); return false;"/>
</p>

<div id="divService">
  <object id="foo" name="foo" type="text/html" data="http://www.example.com/"></object>
    </div>
</form>

Note that now id has the same value as name for text input, and removed this argument for function call. I hope this helps you. (Update: Tested OK on jsFiddle and here is result)


When you make a random number using Math.random, Javascript returns a double. When you put that random number into the URL, you want it to be a string. Try changing the first line of changeChart to this:

var rand_no = String(Math.random() * 1000000);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜