ShareThis

Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Wednesday, October 31, 2012

[Javascript] Print Current Date

Javascript Print Date

Preview:
1
2
3
4
<script type="text/javascript>
var d = new Date();
alert((d.getMonth()+1)+"/"+d.getDate()+"/"+d.getFullYear());
</script>

Wednesday, October 3, 2012

[PHP] Passing PHP parameters from a jQuery Load

I was running into a uncaught exception, error with expression when trying to pass php parameters in a jquery .Load("file.php?a= ... " );

This was occurring because the parameter value for "a" contained spaces.

You can get around this easily with this little hack:

$("#itemwithtext").text().split(" ").join("%20");

This will get the text of the given element, split it into an array delimiting with spaces, then join the array together using the %20 space command, which can be passed safely.

Related solutions

Sunday, July 22, 2012

RadTextBox doesn't update text until box is given focus

This is an annoying problem. We were trying to use jQuery on the ClientID of the control to do:


control.val("text message");


Which will not work.  Instead, you need to use the telerik selector:


$find(controlID);


Then you need to use set_value(...);


textBox.set_value("new text message!");

More information on Telerik's site.

Friday, July 20, 2012

Wednesday, July 18, 2012

How to disable an asp button with runat="server" from javascript

The reason this doesnt work with the asp button's ID is because this ID is not the actual ID on the client side. Instead, you will want to use the button.ClientID.

You can set the client state value of the ClientID on the C# class for the page. In the javascript, you can get the client state value and use jQuery or document.getElementById(...) using the value returned from the client state value.

From there, it is as simple as :



$("#myButton").attr("disabled", "true"); 
$("# myButton ").removeAttr("disabled");


Tuesday, July 10, 2012

[HTML] Where does JavaScript go on an HTML/PHP page?

The best place for Javascript code to go in a HTML or PHP document is at the end. See a discussion on WHY on StackOverflow. Basically, it boils down to the fact that when a browser renders JS, it is in a single-thread, causing the rest of the page to pause in rendering until the JS render is complete. By adding the JS at the end of the page, the content will load first and then the JS will render.

Friday, June 22, 2012

[IE9] Why does Javascript concatenation SUCK in IE9?

This is a public discussion: in my aspx page that uses javascript concatenation, rendering in IE9 kills the entire page. Why does it suck so bad???

Monday, June 4, 2012

Visual studio 2010 javascript comment out shortcut keys


Visual Studio 2008 Full Version:
Comment    ctrl-k, ctrl-c
uncomment  ctrl-k, ctrl-u
Visual Web Developer 2008 Express:
Comment    ctrl-k, ctrl-c
uncomment  ctrl-k, ctrl-u
Visual C# 2008 Express:
Comment    ctrl-e, ctrl-c
uncomment  ctrl-e, ctrl-u

Programmatically check javascript enabled aspx

Use the following example:


<script type="text/javascript">
document.write("Hello World!")
</script>
<noscript>Your browser does not support JavaScript!</noscript>


Source

Sunday, April 15, 2012

[SOLVED] Target="_blank" on a window.location redirect?


Instead of using
window.location='newUrl'
Try:
window.open('newUrl');

Friday, April 13, 2012

HTML Input form restrict to numbers only

Here is an awesome script from htmlcodetutorial.com on how to restrict an input textbox to numbers only. View the source here.



<SCRIPT TYPE="text/javascript">
<!--
// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}

//-->
</SCRIPT>
Now we can create a numbers only field using the onKeyPress attribute. For our first example, we'll create a field which accepts five digits as for a United States ZIP Code. Set the onKeyPress attribute exactly like it is here:
<FORM ACTION="../cgi-bin/mycgi.pl" METHOD=POST>
U.S. ZIP Code: 
  <INPUT NAME="dollar" SIZE=5 MAXLENGTH=5
   onKeyPress="return numbersonly(this, event)">
  <INPUT TYPE=SUBMIT VALUE="go">
</FORM>


Tuesday, November 22, 2011

How to add Blogger Auto Read more with thumbnail

Expandable post or popularly known as “read more” is a feature that make your blog shows only a part of your posts on index pages i.e. home, labels and archive blogger pages. At the end of each preview there will be a link to the post page, usually attached to the phrase “read more”.


This nice trick will automatically create post summaries with thumbnails.You don't need to add any extra code in every blogger post that you make. This is very handy if you have lots of long articles all on one page.

Applying auto read more

Here we go:
  1. Login to your Blogger account.
  2. Go to Dashboard > Design > Edit HTML
  3. Back up your template
  4. Check the Expand Widget Templates check box on top right of the HTML window.
  5. In the code window, look for </head> line.
  6. Add the Read More code below right after that line:

<!-- Auto read more script Start -->
<script type='text/javascript'>
var thumbnail_mode = "yes"; //yes -with thumbnail, no -no thumbnail
summary_noimg = 430; //summary length when no image
summary_img = 340; //summary length when with image
img_thumb_height = 200;
img_thumb_width = 200;
</script>
<script type='text/javascript'>
//<![CDATA[
function removeHtmlTag(strx,chop){ 
 if(strx.indexOf("<")!=-1)
 {
  var s = strx.split("<"); 
  for(var i=0;i<s.length;i++){ 
   if(s[i].indexOf(">")!=-1){ 
    s[i] = s[i].substring(s[i].indexOf(">")+1,s[i].length); 
   } 
  } 
  strx =  s.join(""); 
 }
 chop = (chop < strx.length-1) ? chop : strx.length-2; 
 while(strx.charAt(chop-1)!=' ' && strx.indexOf(' ',chop)!=-1) chop++; 
 strx = strx.substring(0,chop-1); 
 return strx+'...'; 
}

function createSummaryAndThumb(pID){
 var div = document.getElementById(pID);
 var imgtag = "";
 var img = div.getElementsByTagName("img");
 var summ = summary_noimg;
        if(thumbnail_mode == "yes") {
 if(img.length>=1) { 
  imgtag = '<span style="float:left; padding:0px 10px 5px 0px;"><img src="'+img[0].src+'" width="'+img_thumb_width+'px" height="'+img_thumb_height+'px"/></span>';
  summ = summary_img;
 }
 }
 var summary = imgtag + '<div>' + removeHtmlTag(div.innerHTML,summ) + '</div>';
 div.innerHTML = summary;
}
//]]>
</script>
<!-- Auto read more script End -->

Add Auto Read More


Now, find this line: <data:post.body/>

and replace the line with this code:

<!-- Auto read more Start -->
<b:if cond='data:blog.pageType == &quot;item&quot;'>
<data:post.body/>
<b:else/>
<b:if cond='data:blog.pageType == &quot;static_page&quot;'>
<data:post.body/>
<b:else/>
<div expr:id='&quot;summary&quot; + data:post.id'><data:post.body/></div>
<script type='text/javascript'> createSummaryAndThumb(&quot;summary<data:post.id/>&quot;);
</script>
<a class='more' expr:href='data:post.url'>[read more...]</a>
</b:if>
</b:if>
<!-- Auto read more End -->

Click Preview. If it works, then click Save Template. 

Customizing the snippet


You can customize the read more by changing the values of the variables below:
  1. thumbnail_mode: set to “yes” if you want to show thumbnail with text summary. Set to other than “yes” to show only text summary.
  2. summary_img: specify the number of characters (including spaces) you want to show in the summary, with thumbnail.
  3. summary_noimg: specify the number of characters (including spaces) you want to show in the summary, when there is no thumbnail.
  4. img_thumb_height and img_thumb_width: specify the thumbnail height and width (in pixels).
  5. You also can change the words [Read more...] with your own if you want.

That’s it, enjoy!