Jump to content

executing js in the middle of the page


Emwat Oon

Recommended Posts

I'm having a hard time using asp.net's gridview, which basically outputs this html table for me.I'd like to use javascript in one of the columns of this html table, but I would like to avoid using css classes to get the value in this column.Is that possible?I'm thinking along the lines ofJavascript:ConvertNumber( <%# Eval("NumberToConvert") %> )

Link to comment
Share on other sites

can you give it an ID and use document.getElementById? Either way, I wouldn't do it in the middle of the page, i would use window.onload and then get the value after the page is completely ready.

Edited by thescientist
Link to comment
Share on other sites

What do you need the Javascript to do? You could attach event listeners, but I don't think there's an event that would fire on that element when it loads. If you put an onload event handler on a td element, for example, I don't think that would fire. You could put a click event on it, though. So what does the Javascript need to do?

Link to comment
Share on other sites

I need the javascript to convert all the numbers in a gridview column to a human readable day and time.I wanted to make progress, so I went ahead with making a javascript function to go through the column, but it turns out I need help with it...

for (var i = 0, convertValuetoTime = document.getElementById("GridView1").getElementsByTagName("td")[i].innerHTML; typeof convertValuetoTime !== 'undefined'  {            var convertValuetoTime = document.getElementById("GridView1").getElementsByTagName("td")[i].innerHTML;            alert(convertValuetoTime);            i += 9;
Above is a for loop that increments in 9, 9 being right number for the column. When I tried to find the length of the cells, I ended up with less than actual amount of rows. I believe it's due to how the gridview is loaded. I tried a bunch of things and the last thing I tried was the code above: keep going until convertValuetoTime is undefined, which works but I keep getting an error in firebug 'convertValuetoTime is undefined', so that makes me kinda worry about cross-browser compatibility. Edited by Emwat Oon
Link to comment
Share on other sites

I would structure that loop differently. I would just use a normal iteration loop that increments by 9 each time, and inside the body of the loop first check if the value is undefined, and if not then work with it.Keep in mind that this is going to work on the live state of that HTML table. The .NET platform uses a lot of Javascript to change the page, so the table that you're seeing in the browser might not be what you expect.

Link to comment
Share on other sites

Duly noted. Not prepared for the frustration that comes with it though.I structured the code mentioned previously and made it look cleaner.

for(var i = 0; {                var convertValuetoTime = document.getElementById("GridView1").getElementsByTagName("td")[i].innerHTML;                if (typeof convertValuetoTime !== 'undefined') {                    alert(convertValuetoTime);                } else {                    break;                }                i += 9;            }
I still get the TypeError: document.getElementById(...).getElementsByTagName(...) is undefined.
Link to comment
Share on other sites

you should be logging and debugging your code using console.log(). first thing is log document.getElementById('GridVewi1') and make sure that is what you expect it to bethen document.getElementById('GridView1').getElementsByTagName('td');and then after that log the index ()making sure everything is what you expect. To re-emphasise, make sure you are doing this after the DOM has loaded so all elements are there. Logging will inform you of that

Edited by thescientist
Link to comment
Share on other sites

Right, the error is because you're trying to get the value of something that is undefined. So, check the element first. Get just the element:var element = document.getElementById("GridView1").getElementsByTagName("td");And if the element is not undefined, then get the value from it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...