Friday, 1 March 2013

Dynamically loading HTML from another page using Javascript

And here's another great JQuery feature. If you want to include content from another html page you can use JQuery to drag it in. Here's a sample page with a button that pulls in and uses text from a second HTML page.
<html>
    <head>
     <script src="js/jquery-mobile/jquery-1.9.1.min.js" type="text/javascript"></script>
        <script language="javascript" type="text/javascript">
            function a() {
                $('#contentFromOtherPage').load('test2.html #iphone');
            }
        </script>
    </head>
    <body>
    <div id="contentFromOtherPage">
        this text will be replaced
    </div>
    <button onclick= "javascript:a();">click</button>    
</html>
... and here's the second page where the content comes from
<html>
<head></head>
<body>
<div id="iphone">
 this is iphone content
</div>
<div id="android">
 this is android content
</div>
</body>
</html>

Thursday, 28 February 2013

Javascript : How many times was the function called

I recently ran into a problem where my Javascript was looping, and constantly calling a function.  As the browser was locking up, I found it very hard find out the cause of the issue.  To help resolve this issue I  wrote the following.

The comments explain how to use it. In short it records every entrance into a function and counts the instances and pops up an "alert" when a proc is called 50 times (the number is adjstable).


            //
            // You should  place the following lines at the top of your page
            //
            // var functionCalls = new Array();             // used to record all the calls
            // var consecutiveCallCount = 0;                // used to check for consecutive calls    
            // var lastFunctionCalled = '';                 // used to check for consecutive calls    
            // var consecutiveCallCountAlertThreshold = 50; // an alert is generated if a method is called this many times consecutively
            //
            // You should also put an empty DIV as show below, somewhere on the page. The 
            // contents of the div will be updated after every function call, and will 
            // highlight the through put of every function.  If you have a serious loop
            // this will not be updated until the consecutive threshold is reached.
            //
            // 
// // Place a call to this function at the top of every function that you want to track // e.g... // function someFoo () { // storeFunctionCalls(); // ... // // This function will count every call to the function and display it to screen. function storeFunctionCalls() { // get the name of the function that called this one var temp = arguments.callee.caller.toString(); var fName = temp.substring(temp.indexOf("function") + 8, temp.indexOf("(")) || "anoynmous"; fName = fName.replace(/^\s+|\s+$/g, ''); // loop through the existing method calls and update the count var indexOfExistingRow = -1; for (var i = 0; i < functionCalls.length; i++) { if (functionCalls[i].Name == fName) { functionCalls[i].Count++; indexOfExistingRow = i; break; } } // the method that called this has not done so before so add it into the collection if (indexOfExistingRow == -1) { functionCalls.push({ "Name": fName, "Count":1 }); } // update the onscreen display of method calls var s = ''; for (var i = 0; i < functionCalls.length; i++) { s += functionCalls[i].Name + ': ' + functionCalls[i].Count + ''; } $('#debugOutput').html(s); // checks for the consecutive calls and alert if we've passed the threshold. if (fName == lastFunctionCalled) { consecutiveCallCount++; if (consecutiveCallCount == consecutiveCallCountAlertThreshold) { alert(consecutiveCallCount + ' consecutive method calls to the same function :' + fName); } } else { lastFunctionCalled = fName; consecutiveCallCount = 0; } }

Monday, 8 August 2011

IE9 Javascript change

I ran into a problem where by a piece of positioning Javascript that worked in all browser including IE 8 did not work in IE9.  "style.top" did not work under IE9 !

The original code was this...

ele.style.top = (a - b);

I had to change it to...

ele.style.tp = (a - b).toString() + 'px';

Wednesday, 18 May 2011

Avoid Collation Issues

I had yet another query where the difference in the collation between my development database and tempdb that caused a TSQL error to be thrown.

The answer was to "collation proof" the query. It was really simple, you add the collation to be used into the query itself! below is the example.

WHERE #RawData.FullName = DiscrepancyReport.FullName COLLATE SQL_Latin1_General_CP1_CI_AS

Monday, 18 April 2011

Encoding images into HTML using C#

Here's a sample that converts a jpeg into an imline image tag...


using System;
using System.IO;
using System.Drawing;
namespace ImageToBase64
{
class Program
{
static void Main(string[] args)
{
using(System.Drawing.Image i = (Image)Bitmap.FromFile(@"c:\test\TMC_logo_2.jpg"))
{
using(MemoryStream ms = new MemoryStream())
{
i.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = ms.ToArray();
string s = Convert.ToBase64String(bytes);
using(TextWriter tw = new StreamWriter(@"c:\test\string.txt"))
{
tw.WriteLine(string.Format(@"", s));
}
}
}
}
}
}

Friday, 11 February 2011

WatiN Samples

Accessing an HTML Table, which gives access to rows and columns

WatiN.Core.Table tbl = this.IE.Table(Find.ById("ctl00_bodyContentPlaceHolder_GridView"));

Typing into text field

this.IE.TextField(Find.ByName("ctl00$bodyContentPlaceHolder$Name")).TypeText(CompanyName);

Thursday, 1 July 2010

Anonymous Types in a RowDataBound event

Using Linq to SQL to populate a grid

protected void ConsignmentGridView_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
var o = e.Row.DataItem;
Type t = o.GetType();
PropertyInfo pi = t.GetProperty("ConsignmentID");
long ConsignmentID = (long)pi.GetValue(o, null);