I want to achieve functionality to store adsense code into sql database. For that I have used ajax:HtmlEditorExtender on a textbox. now I am typing this code
<script type="text/javascript"><!-- google_ad_client = "ca-pub-*****"; /* 728x90, created 6/12/02 */ google_ad_slot = "****"; google_ad_width = 728; google_ad_height = 90; //--> </script> <script type="text/javascript" src="http://pagead.googlesyndicaon.com/pead/show_ad.js"> </script>
now the problem is when the code is inserted to database, above code is converted as below.
<pre><<span>script</span> <span>type</span>="<a>text/javascript</a>"><!--
<span></span>google_ad_client = "ca-pub-*****";
<span></span>/* 728x90, created 6/12/02 */
<span></span>google_ad_slot = "****";
<span></span>google_ad_width = 728;
<span></span>google_ad_height = 90;
<span></span>//-->
<span></span></<span>script</span>>
<span></span><<span>script</span> <span>type</span>="<a>text/javascript</a>"
<span></span><span>src</span>="<a href="view-source:http://pagead.googlesyndicaon.com/pead/show_ad.js">
http://pagead.googlesyndicaon.com/pead/show_ad.js</a>">
<span></span></<span>script</span>></pre>
Please help me out:
what should I do so that i can store the upper code as it is to database.
when the code is properly stored to database, which control is best suitable to show this ad.(I mean I want to show ad on another page.)
Thanks in advance.
RE issue2: You could use a normal textbox and just decode the string, or put it back into a ajax:HtmlEditorExtender textbox, and use the following code (sanitizer has probably encoded all the tag start and ends etc...
Server.HtmlDecode(TextBox_Editor.Text)
In response to your comment: Try putting the decoded text into a asp:label OnPreRender
EDIT: Try using HtmlAgilityPackSanitizerProvider, the latest pack which resolves some 'bugs' surrounding the sanitizer... have a read of this page, by Stephen Walters own admission the default sanitizer is far too aggressive in what it strips.
Related
I have implemented the latest code mirror in our asp.net application. the editor allows to type javascript which has commented code and un commented code.
I want to know whether all the code written in the editor is commented or not.
Can anyone please advice.
You can fetch the document text, than pass it through a regex, filtering out all the comments:
var editor = CodeMirror.fromTextArea(document.getElementById('code'), {
lineNumbers: true
});
var out = document.getElementById('out');
out.textContent = editor.getValue()
.replace(/(?:\/\*(?:[\s\S]*?)\*\/)|(?:[\s;]+\/\/(?:.*)$)/gm, '');
Demo
I have about 50 background images for my site. What i am looking to do is randomly present the user with a different one for every visit. By this i mean they will surf through the site with the same background image during their visit.
After they close the browser and re-visit or come back and visit later, they then are presented with a new random background image. Don't need to save anything on what their previous background image was, just a random new one for each new visit to the site.
Not sure it this can be done with C#, Javascript, JQuery or CSS.
EDIT: I am using ASP.net 4.0 C# for my web app. Thanks
Don't use cookies as stated in the comments. This will only add extra bandwidth to the header messages sent to the server.
Instead, use local storage in the browser to save what the last image was they used. When a new session is started increment this value, and display the next image.
I've used jStorage on projects and it works fine.
You can save the currently shown image in their browsers storage, and maybe a session ID. Later, you can check if the session ID has changed. If so, then change to a different image.
var image = $.jStorage.get("image", 0);
var session_id = $.jStorage.get("session", "put current session id here");
if(session_id != "current session id")
{
image = (image < 50) ? 0 : image+1;
$.jStorage.set("image",image);
$.jStorage.set("session","current session id");
}
// use image to set background
EDIT:
Don't place this JavaScript in each web page. Instead, place it in a ASP.NET page that responses as a Javascript content type and load it via the page's header. This way page caching on the browser won't affect the script when the session changes.
Keep it in the Session. Pick it at random when it's not already in the session, it will stay the same as long as they're at your site -- and next time they come back, they'll get a new one.
For example (my C# is a little rusty):
public getBackground (HttpSessionState session) {
String bg = (string) session["session.randomBG"];
if (bg == null) {
// pick a random BG & store it.
bg = "pick one";
session["session.randomBG"] = bg;
}
return bg;
}
Hope this helps!
var list = [
"/images01.png",
"/images02.png",
...
];
/*background url*/ = list[Math.floor(Math.random()*list.length];
Sure it is possible. I will use pseudo-code here to show you how it could be done. Surely soon examples in Java will appear.
In the beginning of each page:
StartSession()
If ! SessionVariable[myBackground] then
x=Randomize 50
SessionVariable[myBackground]="image0" + x + ".jpg"
endif
<style>
body {background-image:url(SessionVariable[myBackground];}
</style>
Make sure you use the style tag where appropriate. The SessionVariable[myBackground] is user-created. In PHP it would look like this:
$_SESSION['myBackground']
Best wishes,
Try this function:
/**
* Change background image hourly.
* Name your images with 0.jpg, 1.jpg, ..., 49.jpg.
*/
function getBackground2() {
var expires = 3600000,
numOfImages = 50,
seed = Math.round(Date.now() / expires % numOfImages);
return '/path/to/background/' + seed + '.jpg';
}
I have a public static method string SomeMethod(string message) inside a SomeClass class which gets a string as input and returns something like
<div>message</div>
Of course this is over-simplificated, but the idea is just like this. Now I would like to do something like
<script language="javascript">var string = <% $SomeClass.SomeMethod('asdada');></script>
the first time the page is loaded, but I don't really know how can I do it. That just gives me an "object expected" error in JavaScript. Can anyone please help me out with this?
If you want your server side tags to output something, you should use:
<%: yourCode %>
Your code, invokes the method, but does not send the output to the page:
<script>var string=<% yourMethodCall() %></script>
/* Result = */
<script>var string=</script>
/* ASP.Net code is translated to this serverside code: */
Response.Write("<script>var string=");
yourMethodCall();
Response.Write("</script>");
<script>var string=<%: yourMethodCall() %></script>
/* ^ */
/* Result = */
<script>var string=outputOfYourMethod</script>
/* ASP.Net code is translated to this serverside code */
Response.Write("<script>var string=");
Response.Write(HtmlEncode(yourMethodCall()));
Response.Write("</script>");
And, if this is a serverside class, why do you include the $ sign?
You may put the method to create/register client script dynamically in Page_Load, and check if the page is a postback or not in the Page_Load, and if it's not, run the register script code.
You need to wrap this with quotes and replace the double quotes with the proper escape sequence as well:
var string = "<%=SomeClass.SomeMethod("asdada").Replace("\"", "\\\"")%>";
I am trying to keep track of prints that are made for a page. The page has Print this page link. And the code for it is like below: This is written in .cs file as there are many conditions for displaying this. And i am appending here using String Builder.
sbOutput.AppendFormat("<td align=\"right\" valign=\"bottom\"><div style =\"float:right;text-align:right; valign:bottom;width:200px\"class=\"print_button notPrinted\"><a class=\"notPrinted\" href=\"#\" onclick=\"window.print();\">PRINT THIS COUPON </a><img src=\"images/print-icon-34x34.gif\" class=\"notPrinted\" align=\"absmiddle\" /></div> </td></tr></table>", couponid, Userid, locationid);
Do i have to use onclientclick or something else??
Thanks so much in advance.
Good option would be to write a Javascript function to enable Ajax call, so that you can send a request to server to record the print coomand.
PRINT THIS COUPON
function RecordPrint()
{
// Make a AJAX Call to your server Page to record Print Command.
// printRecorded : success from server
If(printRecorded)
{
window.print();
}
return false;
}
Hope that helps.
since you seem to print on the client side only, you could do
PRINT THIS COUPON
On the server side, implement the counter.aspx page to just count the request and quit silently. Make sure, the window.print() will return true - otherwise the link is not executed.
I am trying to fix some bugs in a product I inherited, and I have this snippet of javascript that is supposed to hilight a couple of boxes and pop up a confirm box. What currently happens is I see the boxes change color and there is a 5 or so second delay, then it's as if the missing confirm just accepts itself. Does anyone smarter than I see anything amiss in this code?
function lastCheckInv() {
document.getElementById("ctl00$ContentPlaceHolderMain$INDet$txtInvNumber").style.background = "yellow";
document.getElementById("ctl00$ContentPlaceHolderMain$INDet$txtInvNumber").focus();
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_AddCharges").style.background = "yellow";
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_lblFreight").style.background = "yellow";
bRetVal = confirm("Are you sure the information associated with this invoice is correct?");
return bRetVal;
}
The only thing I can think of is if one of the lines before the confirm is throwing an exception and you're never actually getting to the confirm.
If you're using IE, make sure script debugging is enabled. If you're using Firefox, install the Firebug add-on and enable it for your website.
Or, for very primitive debugging, just put alerts after each statement and figure out where it's bombing.
You should use the following method to reference your controls from JavaScript:
document.getElementById(<%= txtInvNumber.ClientID %>).style.background = "yellow"
If that doesn't help, try setting a breakpoint in your JavaScript and stepping through it to see where it's failing.
This test script ran fine for me in IE, Firefox, and Opera. So there doesn't seem to be anything wrong with your basic syntax. The problem is either in the ID's (which doesn't fit with the fact that it acts as if confirmed after 5 seconds) or in some other conflicting JavaScript on the page. It will be difficult to help you without seeing more of the page.
<script language="javascript">
function lastCheckInv() {
document.getElementById("test1").style.background = "yellow";
document.getElementById("test1").focus();
document.getElementById("test2").style.background = "yellow";
document.getElementById("test3").style.background = "yellow";
bRetVal = confirm("Are you sure?");
return bRetVal;
}
</script>
<form method="get" onsubmit="return lastCheckInv();">
<input id="test1" name="test1" value="Hello">
<input id="test2" name="test2" value="Hi">
<input id="test3" name="test3" value="Bye">
<input type="submit" name="Save" value="Save">
</form>
A few thoughts: Could be the .focus() call is hiding your confirm behind the page? Or could it be that one of your control id's is not correct causing, the .style.background references to fail?
You should set the focus after showing the confirm box, otherwise the confirm box will grab focus away, making that line meaningless.
Don't hard-code ASP.Net id's like that. While they typically are consistent, a future version of ASP.net won't guarantee the same scheme, meaning your setting yourself up for pain when it comes time to update this code at some point in the future. Use the server-side .ClientID property for the controls to write those values into the javascript somewhere as variables that you can reference.
Update:
Based on your comment to another response, this code will result in a postback if the function returns true. In that case, there's not point in running the .focus() line at all unless you are going to return false.
I do not like accesing objects directly by
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_lblFreight").style.background = "yellow";
If the object is not returned JavaScript will error out. I prefer the method of
var obj = document.getElementById("ctl00_ContentPlaceHolderMain_INDet_lblFreight");
if(obj)
{
obj.style.background = "yellow";
}
My guess is you are trying to access an object that is not on the DOM, so it never gets to the confirm call.
It might be worth checking that the elements actually exist. Also,try delaying the focus until after the confirm():
function lastCheckInv() {
var myObjs,bRetVal;
myObjs=[
"ctl00_ContentPlaceHolderMain_INDet_AddCharges",
"ctl00_ContentPlaceHolderMain_INDet_lblFreight",
"ctl00$ContentPlaceHolderMain$INDet$txtInvNumber"
];
bRetVal = confirm("Are you sure the information associated with this invoice is correct?");
for (var i=0, j=myObjs.length, myElement; i<j; i++){
myElement=document.getElementById(myObjs[i]);
if (!myElement){
// this element is missing
continue;
}
else {
// apply colour
myElement.style.background = "yellow";
// focus the last object in the array
if (i == (j-1) ){
myObj.focus();
}
}
}
return bRetVal;
}
function lastCheckInv()
{
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_txtInvNumber").style.background = "yellow";
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_txtInvNumber").focus();
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_AddCharges").style.background = "yellow";
document.getElementById("ctl00_ContentPlaceHolderMain_INDet_lblFreight").style.background = "yellow";
return confirm("Are you sure the information associated with this invoice is correct?");
}
The first two lines in your function are wrong, $ are in the UniqueID of an ASP.Net Control.
On Clientside you have to use the ClientID, replace $ with _ .
If you are sure that the Controls exists, the
code above might work.
Is bRetVal actually declared anywhere?
If not, "var bRetVal = confirm...." is a friendly way of telling jscript that it is a variable.