How can I detect the end of a webpage to put a div bar that contains the 'about' and 'contact us' links? I want to detect the end of the page so the bar can automatically put itself at the end of the page regardless the resolution. Thanks
If you want implementation in pure JavaScript try:
var node = document.createElement("div");
node.innerHTML = "put html here";
document.body.appendChild(node);
Here is the jsbin snippet.
Another possible solution is:
document.body.innerHTML += "put html here";
You could do this with a master page, or I guess since you tagged this with javascript you could use jquery as well. Use a selector to select the body or html tag and then use append:
http://api.jquery.com/append/
$("body").append("Your links");
Well, you can find the </body> tag, and add your code immediately before it, being sure it will be rendered at the very end of the web page.
window.pageYOffstet + window.screen.height == document.height
Related
What I'm doing is grabbing the HTML code for my header from a text file.
But once a User logs in I want it to say Welcome "Username" at the top, which is a dropdown to account settings, cart, etc...
So since I'm inserting the HTML into a DIV on page load, I don't actually have access to any of the elements inside in C#.
How would I go about doing this? Is there any way to access something like a (p id="name")'s inner text, after its loaded in from the text file?
Would like to do this with C# not JS please.
Edit: I have a work around for now, but I am still interested in better answers.
headerText = headerText.Replace("::Username::", Session["Username"] as string);
Here is my code for grabbing the HTML and pasting it in.
string headerText = File.ReadAllText(Server.MapPath("~/global/header.html"));
string footerText = File.ReadAllText(Server.MapPath("~/global/footer.html"));
headerText = headerText.Replace("::Username::", Session["Username"] as string);
divHeader.InnerHtml = headerText;
divFooter.InnerHtml = footerText;
To be more clear, is there anyway to access something like
<asp:Panel ID="panelAccount" runat="server">
which is stored in another HTML file.
I have done similar work in C# earlier, I use HTMLAgilityPack for doing this kind of works. Later I start using Anglesharp since it has a very good CSS selector based support.
Try anglesharp and you can modify the HTML tag like you do in jQuery.
As much as I liked Adrians response, I found out a better way to do what I needed to do, I could do with a MasterPage. I definitely be using Adrians answer for a load of other things though so it still holds valid.
Then in the C# file associated with the master page you create a property like this.
public Panel PanelAccount
{
get { return panelAccount; }
set { panelAccount = value; }
}
Then from the regular WebForm, you can call "PanelAccount" to access that property.
Here is a tutorial for how to do that.
Thanks for everyones downvotes with no inputs, you guys are stars!
I have a codebehind file, in which it does a:
Response.Redirect(Request.RawUrl);
after i have updated something in the database.
(when a comment for the topic is stored)
I wish to make the page autoscroll, to the bottom at the page, when the response is triggered.
Can someone please tell me how?
Scrolling to the bottom of the page can be done by using javascript. Below is the javascript code to do the same. Please place the below code at the end of the page before body tag ending.
<script>
window.scrollTo(0, document.body.clientHeight);
</script>
What about adding an anchor to the new comment.
<a name="NewComment">The comment</a>
Then let your redirect point to that anchor
Response.Redirect(Request.RawUrl + "#NewComment");
That should make your browser scroll to that anchor
Scrolling to the bottom of the page is client side not server side. C# is server side. In order to scroll you will have to add some javascript to the page to do it for you.
Typically, this is implemented as follows, a javascript routine is written that looks at the URL. If there is some specific information in the url (eg &scroll2bottom=true) then the javascript performs that action. This will also let you scroll to a specific page element.
I use this function, maybe it is useful for you. It works with coordinates
and delay seconds for scroll. Trying with different coordinates will make the scroll
stop where you want.
Declare this function in your js
function WindowsScrollTopAnimado(coordinate,miliseconds) {
$('html, body').animate({scrollTop:coordinate}, miliseconds);
}
Call it from server side when needed:
Private Sub ScrollToElement()
Dim Cadena = "<script type='text/javascript'>"
Cadena += "WindowsScrollTopAnimado( " & 1350 & "," & 1800 & ");"
Cadena += " </script>"
ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "ScrollToControl", Cadena, False)
End Sub
I would insert an anchor into the HTML at the point you want to scroll to (this can be permanently in the code at the bottom, or inserted dynamically at the target point), and then redirect to yoururl.html#anchor
Unfortunately that's not very HTML5/Web2.0, the new modern way appears to be here: http://dev.w3.org/html5/spec/single-page.html#scroll-to-fragid
I've had some fun with this. A simple code-behind way I use is to put focus on the last element on the screen such as an empty label.
E.g.
lblMyEmptyLabel.Focus();
Of course, this doesn't smoothly scroll the window, it pretty much "teleports" you there.
Sometimes the above doesn't work (when using bootstrap modals for example) in which case the following javascript works for me:
<script type="text/javascript">
function openModal() {
document.getElementById('myElement').scrollIntoView(true);
$('#myModal').modal('show');
}
</script>
If you're using an asp element (eg a textbox) to scroll to, don't forget to add the clientidmode="static" part to the tag so the ID isn't altered on the client.
I am trying to write email on a web page. But, I want to clear the textbox boefore writing on page. Because, if any other email is there in the textbox, the below code is not working.
How it is possible to clear the textbox in the web page using html element?
HtmlElement ele = webBrowser.Document.GetElementById("email");
if (ele != null)
ele.InnerText = "myacc#gmail.com";
try using the value attribute for clearing it
hmm... if you mean textfield & javascript code:
<input type="text" id="email">
then you can use simply smth like this:
document.getElementById('email').value = 'myacc#gmail.com';
or i am misunderstanding your question.
P.S. Oops. It seems not like javascript, sry.
Try this:
ele.SetAttribute("value", "");
Another option is to use Document.InvokeScript to call a Javascript function that clears the text box.
Is it possible to scrape all the text from a site that was navigated to by WebBrowser control without looking at the source?
David Walker's method is great when one don't need any info from the header nor non main part of the webpage. if one need something outside inner text, there is only two options, one is to parse with "getElement".
the other one is issue commands (Document.ExecCommand) to webbrowser to select all and copy to clipboard:
wb.Document.ExecCommand("SelectAll", false, null);
wb.Document.ExecCommand("Copy", false, null);
then finally string content=clipboard.getText();
Please note the spelling and syntax may not be correct, I'm recalling from my memory
string browserContents = webBrowser.Document.Body.InnerText;
You use the DocumentText property or the WebBrowser control.
This property is what holds the HTML of the site you have navigated to.
Update: (following comments)
If you want to parse the HTML and get the text parts of it, I suggest you use the HTML Agility Pack.
Hi I tried to read a page using HttpWebRequest like this
string lcUrl = "http://www.greatandhra.com";
HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);
loHttp.Timeout = 10000; // 10 secs
loHttp.UserAgent = "Code Sample Web Client";
HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();
Encoding enc = Encoding.GetEncoding(1252); // Windows default Code Page
StreamReader loResponseStream =
new StreamReader(loWebResponse.GetResponseStream(), enc);
string lcHtml = loResponseStream.ReadToEnd();
mydiv.InnerHtml = lcHtml;
// Response.Write(lcHtml);
loWebResponse.Close();
loResponseStream.Close();
i can able to read that page and bind it to mydiv. But when i click on any one of links in that div it is not displaying any result. Because my application doesnt contain entire site. So what we will do now.
Can somebody copy my code and test it plz
Nagu
I'm fairly sure you can't insert a full page in a DIV without breaking something. In fact the whole head tag may be getting skipped altogether (and any javascript code there may not be run). Considering what you seem to want to do, I suggest you use an IFRAME with a dynamic src, which will also hopefully lift some pressure off your server (which wouldn't be in charge of fetching the html to be mirrored anymore).
If you really want a whole page of HTML embedded in another, then the IFRAME tag is probably the one to use, rather than the DIV.
Rather than having to create a web request and have all that code to retrieve the remote page, you can just set the src attribute of the IFRAME to point ot the page you want it to display.
For example, something like this in markup:
<iframe src="<%=LcUrl %>" frameborder="0"></iframe>
where LcUrl is a property on your code-behind page, that exposes your string lcUrl from your sample.
Alternatively, you could make the IFRAME runat="server" and set its src property programatically (or even inject the innerHTML in a way sismilar to your code sample if you really wanted to).
The code you are putting inside .InnerHtml of the div contains the entire page (including < html >, < body >, < /html > and < /body> ) which can cause a miriad of problems with any number of browsers.
I would either move to an iframe, or consider some sort of parsing the HTML for the remote site and displaying a transformed version (ie. strip the HTML ,BODY, META tags, replace some link URLs, etc).
But when i click on any one of links in that div it is not displaying any result
Probably because the links in the download page are relative... If you just copy the HTML into a DIV in your page, the browser considers the links relative to the current URL : it doesn't know about the origin of this content. I think the solution is to parse the downloaded HTML, and convert relative URLs in href attributes to absolute URLs
If you want to embed it, you need to strip everything but the body part. That means that you have to parse your string lcHTML for <body....> and remove everything before and includeing the body tag. You must also strip away everything from </body>. Then you need to parse the string for all occurences of <a href="....."> that do not start with http:// and include h t t p://www.greatandhra.com or set <base target="h t t p://www.greatandhra.com"> in your head section.
If you don't want to embed, simply clear the response buffer and stream the lcHTML string back to the browser.
PS: I had to write all h t t p with spaces to be able to post this.
Sounds like what you are trying to do is display a different site embedded in your site. For this to work by dropping it into a div you would have to extract the code between the body tags as it wouldn't be valid with html and head in the middle of another page.
The links won't work because you've now taken that page out of context in your site so you'd also have to rewrite any links on the page that are relative (i.e. don't start with http) to point to a page on your site which will then fetch the other sites page and display them back in your site, or you could add the url of the site you're grabbing to the beginning of all the relative links so they link back to that site.