I just want to change the URL on Runtime? - c#

I am having anchor tag referring to a url i.e
<a taget='_blank' href='http://localhost:4850/en/abc.xml'>
I just want on runtime after clicking on this hyperlink the website will go to
http://localhost:4850/abc.xml
instead of
http://localhost:4850/en/abc.xml
i.e the "en" from the url is removed.
I am having a .Net Application

Assume that your anchor tag with id is like
<a taget='_blank' id="url" href='http://localhost:4850/en/abc.xml'>
Using javascript, on clicking your anchor tag
$('#url').click(function(e){
e.preventDefault();
window.location.href = 'http://localhost:4850/abc.xml';
});
It will redirect you to respected url.

Try this:-
Your anchor tag is like this:-
<a taget='_blank' href='http://localhost:4850/en/abc.xml' id="anc1">Test</a>
Use this script
<script src="https://code.jquery.com/jquery-1.12.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("a[ID$='anc1']").click(function () {
var url = $(this).attr("href");
var newurl = url.replace("/en", "");
$(this).attr("href", newurl);
});
});
</script>

Related

Link Dynamic URL to div in Umbraco

I am building a site using Umbraco and I have a div that currently has a link inside it. The current code is:
<div class="callout col-sm-4 leftCtaLink">
<p>#CurrentPage.leftDescription</p>
<a class="primary-bg" href="#CurrentPage.leftCtaLink"><img class="svg-inject" src="#Umbraco.Media(CurrentPage.leftIcon).umbracoFile" alt="Icon" />#CurrentPage.leftCtaText</a>
</div>
This works and only the bottom half of the callout links to the correct page. The client wants the whole div to be linkable now though, so I thought I'd do this with jQuery. Here is that:
<script type="text/javascript">
$(document).ready(function () {
$(".leftCtaLink").click( function() {
window.location=$(this).find("a").attr("#CurrentPage.leftCtaLink");
return false;
});
});
</script>
The issue is, that when the div is clicked on, it takes me to the website's url with /undefined at the end of it. Can anyone tell me what I need to change in the JS to have it use the correct URL that was input in the CMS?
Change to attr("href").
$(document).ready(function () {
$(".leftCtaLink").click( function() {
window.location=$(this).find("a").attr("href");
return false;
});
});

Open a tab with a URL when link clicked

I want to open a new tab in the same browser when the 'Terms and Conditions' link is clicked which should return the URL: http://localH:30321/OrchardLocal/TermsAndConditions
So Far I have:
<p>Accept the <span class="big-red">Terms and Conditions</span></p>
<script>
function newwin() {
window.open("~/TermsAndConditions");
}
</script>
This actually opens 2 new tabs in the same browser.
http://localH:30321/OrchardLocal/Users/Account/~/TermsAndConditions
which throws a page cannot be found for obvious reasons
ad then the second tab shows the home page:
http://localH:30321/OrchardLocal/
Tried removing the href and one of the ~/...cant seem to get it...any help guys?
thanks
First of all sorry for the wrong answer:
As you want to open the url in new tab of same browser Which is not possible.It all depends on the browser settings.
http://forums.asp.net/t/1902352.aspx
Open a URL in a new tab (and not a new window) using JavaScript
IF browser setting are default(tested for FF)
this will work Open new tab in same browser window
function openinnewTab() {
var win = window.open("~/TermsAndConditions", '_blank');
win.focus();
}
try this
function newwin(e) {
e.preventDefault();
window.open("/TermsAndConditions", '_blank');
}
by the way you cant use directly "~" in javascript..if you want to use server code like this
function newwin(e) {
e.preventDefault();
window.open('<%= ResolveUrl("~/TermsAndConditions") %>', '_blank');
}
EDIT
<p>Accept the <span class="big-red">Terms and Conditions</span></p>
JS
function newwin(e) {
window.open('<%= ResolveUrl("~/TermsAndConditions") %>', '_blank');
}
or
function newwin(e) {
window.open("/TermsAndConditions", '_blank');
}
OR pure asp.net code
<p>Accept the <a runat="server" href="~/TermsAndConditions" target="_blank" > <span class="big-red">Terms and Conditions</span></a></p>
you have to add runat="server" attribute
<a href='JavaScript:newwin('<%=ResolveClientUrl("~/OrchardLocal/TermsAndConditions")%>');' >
and
<script>
function newwin(url) {
window.open(url,'_blank');
}
</script>
One tab is for your href and the other for your onclick. sajad-lfc gave the rigth answer.
Try the following piece of code
<p>Accept the <span class="big-red">Terms and Conditions</span></p>
<script>
$("span.big-red").on("click", function () {
window.open('www.yourdomain.com', '_blank');
});
</script>
This is working perfectly hopefully works for you.

how to call C#(aspx.cs) function by using java script/Jquery by passing parameters

.aspx
-----------------------
<head>
<script type="text/javascript">
>> $(document).ready(function () {
$(".cssBtns").click(function () {
// call aspx.cs method generateInfo(this.id,this.className)
});
});
</script>
</head>
<body>
</body>
.aspx.cs
-----------------------
public void generateInfo(int btnId, string className){
print:// css button id:---
css button class:----
}
how can i call generateInfo method which is aspx.cs page by using java script/JQuery. and which is the best way.
You can use $.ajax(), $.post(), $.get() or $.getJSON() for doing this.
$(".cssBtns").click(function () {
$.ajax({
url : 'aspx function path here'
});
});
See the link below for more reference.
how to call an ASP.NET c# method using javascript
http://api.jquery.com/jQuery.ajax/

Setting default text in WYMEDITOR

I've got a question about using WYMEditor in ASP.NET MVC 3 with jQuery. I'd like to set default text in WYMEditor on my web page. If I'm doing in that way :
<script type="text/javascript">
jQuery(function() {
jQuery(".wymeditor").wymeditor( { html : '<strong>some text</strong>'});
});
There is no problem, and wymeditor shows well-formated text, but is I try it in that way :
<script type="text/javascript">
jQuery(function() {
jQuery(".wymeditor").wymeditor( { html : '#ViewBag.HtmlText'});
});
(HtmlText is variable where I keep : <strong>some text</strong>) the WymEditor shows me not formated text <strong>some text</strong>. I tried HtmlEncoding and etc, but it stil isn't working.
Try like this:
<script type="text/javascript">
jQuery(function() {
var html = #Html.Raw(Json.Encode(ViewBag.HtmlText));
jQuery('.wymeditor').wymeditor({ html: html });
});
</script>
And please get rid of this ViewBag as everytime I see it I get sick. Use view models and strongly typed views.

in asp mvc, can I dynamically generate javascript?

On my aspx view, I would like to generate javascript where some parts are generated:
before generation:
<script type="text/javascript">
var A = 'an id';
var B = "http://www.yahoo.com" + <%= Model.pathname %>;
</script>
After generation:
<script type="text/javascript">
var A = 'an id';
var B = "http://www.yahoo.com/videos/index.htm" ;
</script>
is this possible? what options do I have?
I suggest the following code:
<script type="text/javascript">
var A = 'an id';
var B = "http://www.yahoo.com<%= Model.pathname %>";
</script>
Maybe the IntelliSense is not completely right in Visual Studio, but it will work.
yes, it's entirely possible, javascript is not executed untill way after all of this stuff is rendered, you have pretty much whatever options you can imagine.
Yes this should work fine, just surround the directive with single quotes for example:
<script type="text/javascript">
var A = 'an id';
var B = "http://www.yahoo.com" + '<%= Model.pathname %>';
</script>
Yes, that's possible.
If the JavaScript code is in your view, then simply doing the: <%= Model.pathname %> would work.

Categories

Resources