HTMLAgilityPack Add to end of script - c#

I need to use HTMLAgilityPack to add a line into jQuery(document).ready of something like this
<script type="text/javascript">
jQuery(document).ready(function($) {
$...
});
</script>
How can I select that specific node?

In respond to updated question, to find the <script> tag containing javascript code, you can try using this XPath :
//script[#type='text/javascript']
or if you need to also make sure javascript code within that tag has jQuery(document).ready part :
//script[#type='text/javascript' and contains(.,'jQuery(document).ready')]

Related

C# Anchors Passing Values

I'm trying to create a A href that will take a value to the same page, it will just fill a textbox hidden. It's pretty much for comment threading, and i want to use the Reply A href to generate which reply it's going to. Anyone have any suggestions?
I have tried but doesn't seem to fill the textbox it just refreshes the page.
Use javascript within the link:
Do This
<script type="text/javascript">
function _callFunction(link) {
document.getElementById("hiddenID").value = "X";
}
</script>

Add new class and attributes to div if it exists on the page [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to add new class and attributes to div if it exists on the page
I need JavaScript code on my master page which tries to find if a div exists. If so, it should add a new class and also add a new id attribute.
For example if the page has this div:
<div class="toplink">abc..</div>
Then JavaScript code should make it exactly like this:
<div class="toplink adin" data-aid="114">abc..</div>
The code inside the div should remain the same.
I tried this code but this is not working
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js" type="text/javascript">
if ($('.toplink').exists()) {
$('.toplink').addClass('adin').attr('data-aid', '114');
}
</script>
What wrong with this code? what where i placed this code,in header on footer?
Can i do this with javascript, not jquery
Instead of
.attr('data-aid','114')
use the .data method:
.data('aid','114')
http://api.jquery.com/data
Try $().length property instead, and place this snippet at the very bottom of the page, before closing body tag
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
if ($('.toplink').length > 0) {
$('.toplink').addClass('adin').attr('data-aid', '114');
}
});
</script>
Your code is probably just running before the DOM is ready. wrap as follows:
$(function() {
$('.toplink').addClass('adin').attr('data-aid', '114');
});

Encoding and decoding between Javascript and c#

I want to be able to send data to Javascript in a div like this:
<div class="dontDisplay><%= myData %></div>
However, the challenge is that the HTML will get messed up. There seems to be a plethora of encoding and decoding schemes out there, but I can't find a javascript/C# pair that are guaranteed to be compatible for all possible inputs (including Unicode.)
Does anyone know of a pair that are known to be perfectly compatible?
Thanks
You most probably want to use json. You can embed a string in your page that creates a local variable on your page using json inside a script-tag:
<script> var myJsonVariabe = { propertyNane: "value" }; </script>
You can serialize a .net object to json using the DataContractJsonSerializer.
You appear to be just putting some text into HTML text content, for which the correct approach is plain old HTML-encoding:
<div class="dontDisplay"><%= Server.HTMLEncode(myData) %></div>
or in ASP.NET 4.0:
<div class="dontDisplay"><%: myData %></div>
If JavaScript needs to extract this, you can use DOM methods:
<div id="foo"><%= Server.HTMLEncode(myData) %></div>
<script type="text/javascript">
var myData= document.getElementById('foo').firstChild.data;
</script>
although this assumes that there's exactly one text node in there, and will break if myData is an empty string. You can work around this by having a getTextContent method that checks all the childNodes for being text nodes and adds their data together, or use textContent with fallback to innerText for IE, or use a library to do it (eg. text() in jQuery).
alternatively you can put the data in an attribute:
<div id="foo" title="<%= Server.HTMLEncode(myData) %>">...</div>
<script type="text/javascript">
var myData= document.getElementById('foo').title;
</script>
if there is a suitable attribute available (title might not be appropriate), or a comment, or directly into a JavaScript variable using JSON as suggested by Rune.

"Expected expression" with <%=foo%> inside JavaScript in .aspx file

I've got code like this in an .aspx file:
<script type="text/javascript" language="javascript">
function init()
{
<%= x %>
}
It works fine (x is a string that will be bound to some JavaScript at runtime), but when compiling I get an "Expected expression" warning on the <%=
I know it's not the nicest code in the world, but there are various historical bits of code that like to inject little bits of JavaScript into the page. All perfectly innocent :)
The warning is happening because the code block is inside a JavaScript <script> block; the compiler is trying to be smart about recognizing the difference between HTML/controls and JavaScript.
Although it's a bit ugly, you should be able to eliminate the warning by using eval('<%= x %>')
You might also have a look at an article I wrote on using ASP.NET to create dynamic JavaScript: http://www.12titans.net/p/dynamic-javascript.aspx
If you wanted to be perfectly legal, use a literal:
<asp:Literal ID="litX" runat="server"></asp:Literal>
and set its Text to the whole <script type="text/javascript" language="javascript">...</script> block on the server side.
Visual Studio is validating the HTML markup on the page. Strictly speaking, the '<' is not valid XHTML. This warning may be caused by a limitation of the validation as Visual Studio is interpreting the '<' character inside javascript as meaning 'less than'!
Obviously these inline expressions are not valid client code. One can change the HTML validation options in Tools|Options|Text Editor|HTML. Mind you, it may be better to simply ignore these warnings rather validate against HTML 4.01 or turn off the validation completely.
If all you're doing is adding javascript to your page, you could use RegisterClientScriptBlock to build the script server-side and then let it output it to the page.
This way you don't have to have any server-side scripting within your page and all the overhead that goes with that.
I've done this before and not had problems. However, I use jQuery's $(document).ready() instead of init(). I don't think that should be an issue, though.
What datatype is x? If it is a string, try wrapping it in single quotes, and if it's a number use the parse function for that datatype.
<script type="text/javascript" language="javascript">
var s;
var n;
function init()
{
s = '<%= x %>';
n = parseInt('<%= x %>');
eval('<%= x %>'); /* Raw Javascript */
}
</script>
Another issue would be the access level of the property. ASP.NET web forms can't access private fields directly from the code-behind. Access has to be protected or public.
Eval Javascript Function
I found the problem that I was having with this.
It seems I had incorrectly commented out HTML above the script.
<%-- <div id="directionpanel" ></div>--%>
<script type="text/javascript">
var LIST = <%= getJson()%>;
</script>
I was getting the expected expression warning on getJson()
Correctly commenting the HTML will fix the warning.
-->

JQuery in ASP.Net User Control in C#

I'm having problems using JQuery inside an ASP.Net User Control I've created a sample and here is the markup for my user control:
<%# Control Language="C#" ClassName="UC" AutoEventWireup="true"
CodeFile="UC.ascx.cs" Inherits="UserControls_UC" %>
<span id="Licenses"></span>
<script type="text/javascript">
$(document).ready(function() {
var ctlID = $("span[id$='Licenses']");
ctlID.text = "Testing";
});
</script>
If I include this script tag <script type="text/javascript" src="js/jquery-1.3.2.js" /> in the aspx file containing the user control to reference JQuery, nothing happens. If I don't include it, I get a JavaScript error dialog saying there was a runtime error and that an Object was expected. Any ideas what I may be doing wrong?
text is a function in jQuery. Try:
ctlID.text("Testing");
Prefixing the '$' to the id gets all elements that ends with "Licenses" .
Make a quick change to test this to '#' will get you exactly one element.
$(document).ready(function() {
var ctlID = $("#Licenses");
ctlID.text = "Testing";
});
To make it work with attribute selector try
$('span[#id=Licenses]') // You can omit the # in the latest version of jquery.
I think you should use something like this:
var ctlID = $("span[id$='Licenses']").get(0);
ctlID.text = "Testing";
Have you included the jquery library correctly on master page?
Is that not meant to be dollar before id, i.e. $id='Licenses' ? or even a #

Categories