URL redirection is not working i.e
I am using filter for replacing string which match with my anchor tag.
DEMO
Controller
var app = angular.module('app', ['ngSanitize']);
app.filter('parseUrlFilter', function ($sce) {
var urlPattern = /(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi;
return function (text, target, otherProp) {
text = text.replace(urlPattern, '<a target="' + target + '" href="$&">$&</a>');
return $sce.trustAsHtml(text);
};
});
function Ctrl($scope) {
$scope.text = 'Example text http://example.com http://example.com http://google.com google.com';
}
HTML
<div ng-controller="Ctrl">
<p ng-bind-html="text | parseUrlFilter:'_blank'"></p>
</div>
My problem is when url having http or https then it redirect us correctly to respective page i.e if "http://google.com" it works properly. But if
http or https is missing then it does not work properly i.e if google.com it doesn't redirect to google.com instead it redirect to localhost/folder/google.com.
Related
I am trying to replace all plain links with hrefs and remove the beginning part of the url.
The current code I am working with finds https and http links and replaces them, but it doesn't work with www. plain links.
var links = $('#links').text();
var formatText = links.replace(/((http|https|www):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g, '$1<br>');
$('#links2').html(formatText);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="links">
https://www.example.com/<br>
https://www.example.com/blah/blah/<br>
http://www.example.com/<br>
http://www.example.com/blah/blah<br>
www.example.com/<br>
www.example.com/blah/blah
</div>
<br>
<div id="links2"></div>
I would also like the https, http and www. removed from the link text aswell, for example:
example.com/
example.com/
Does anyone know how?
You can use
/^.*?\.(.*)/
If you want the original link in the href attribute then you can use ${link} or if you want the link with domain only then you can use ${group}
const links = [
"https://www.example.com/",
"https://www.example.com/blah/blah/",
"http://www.example.com/",
"http://www.example.com/blah/blah",
"www.example.com/",
"www.example.com/blah/blah",
];
const result = links.map((link) =>
link.replace(/^.*?\.(.*)/, (...args) => {
const [, group] = args;
return ` ${group}<br>`;
})
);
console.table(result);
Hello I am trying to replace the href of three hyperlinks. Which I can do with the following code:
var url = $('.contact_link').attr('href');
url = url.replace('http://www.contact.nl/', '/');
$('.contact_link').attr('href', url);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Privacypolicy | Privacy statement |
Copyright test| Contact
Which result in a change of each hyperlink containing the contact_link class. But each hyperlink is only updated with the first href. So each hyperlink now contains the URL: "/Privacypolicy.aspx"
How could I achieve that each URL is updated with their corresponding href?
You can use each
$('.contact_link').each(function() {
var url = $(this).attr('href');
url = url.replace('http://www.contact.nl/', '/');
$(this).attr('href', url);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Privacypolicy | Privacy statement |
Copyright test| Contact
Hello I am super new to building websites. Please excuse my lacking terminology!!
I have a website that has Wildcard sub-domains. It is using this script to pull the wildcard sub-domains usernames.
<p id="dist-info"></p>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
return;
var get_data_url = 'https://backoffice.WEBSITE.com/api/v2/public/users/{username}';
$.getJSON(get_data_url, function( data ) {
var dist_info = "<p>"+data.response['first-name']+"</p>" +
"<p>"+data.response['last-name']+"</p>" +
"<p>"+data.response['distributor-id']+" "+data.response.email+"</p>" +
"<p>"+data.response['image-url']+"</p>" +
"<p>Phone: "+data.response.phone+"</p>";
$('#dist-info').html(dist_info);
});
});
</script>
Now I need to make a URL that will parse the username/user id out of the Wildcard Subdomain page. What code do I need to use?
For example
The URL is
USERNAME.WEBSITE.com/page/subpage/
I need to make this URL
backoffice.WEBSITE.com/page?sponsor-id=USERNAME
What do I need to do so that the username from the first page is parsed out and applied to the link of the second URL
You want to modify this JavaScript so it uses the subdomain instead of {username}?
$(document).ready(function() {
var get_data_url = 'https://backoffice.WEBSITE.com/api/v2/public/users/';
var hostname_parts = location.hostname.split('.');
var username = hostname_parts.shift();
// add username to the data URL
get_data_url += username;
// add username to some link on the website
var $aTag = $('#id-of-the-link');
var link_url = $aTag.attr('href');
link_url += username;
// set the new href attribute
$aTag.attr('href', link_url);
location.hostname gives you USERNAME.WEBSITE.com, split() splits it into parts separated by a dot. shift() takes the 1st element of this array (you could also use hostname_parts[0]) and with += you concatenate it to the URL.
The second example shows how to add the username at the end of a link like
click
Edit: added example for changing a href attribute
I'm looking to get a URL from a query. (The query is "?url=urlgoeshere" [where "urlgoeshere" is the URL to redirect to]) and redirect to that URL. For some reason when I run the script it goes to "http://mydomain.ext/urlgoeshere" (where "urlgoeshere" is the URL to redirect to)
Any ideas on how I could fix this issue?
This is my current code:
var loc = window.location.href;
var url = window.location.search.split("?url=");
var countdown=5
var currentsecond=document.rd.box.value=countdown+1
function redirect(){
if (currentsecond!=1){
currentsecond-=1
document.rd.box.value=currentsecond
} else {
open(url);
return
}
setTimeout("redirect()",1000)
}
redirect()
<form name="rd">
<center>
<br><br><br><br>
<font face="Arial"><b>You will be redirected in<br><br>
<input type="text" size="3" name="box">
</form>
seconds</b></font>
</center>
After splitting your url. Your url will be an array of two entries.
url[0] will be your current page url.
url[1] will be the url you want to redirect.
So in your code use the following:
open(url[1])
Using AngularJS, Im trying to load the "myLink" URL address to iframe in another html.
data.No is the id that i pull from another place and works fine (get the Id that i need for the url)
in the controller - "TransactionsCtrl":
$scope.myLink = "http://"the real url"+ data.No +"&file="+ data.No +"&contract_id="+ data.No;
console.log($scope.myLink);
in the HTML :
<div ng-controller= "TransactionsCtrl">
<iframe ng-src="{{myLink}}"></iframe>
</div>
and all i get is this :
Error: [$interpolate:interr] Can't interpolate: {{myLink}}
Error: [$sce:insecurl] Blocked loading resource from url not allowed by $sceDelegate policy. URL
when i hard coded the url its working fine.
In the controller you should use:
$scope.myLink = $sce.trustAsResourceUrl(myUrl)
It works for me : you can write this in js code as a function
$scope.trustSrc = function(src) {
return $sce.trustAsResourceUrl(src);
}
$scope.iframe = {src:"http://www.youtube.com/embed/Lx7ycjC8qjE"};
and use it in your view :
<iframe ng-src="{{trustSrc(iframe.src)}}"></iframe>
or you can write it as a filter like :
.filter('trusted', function($sce){
return function(url) {
return $sce.trustAsResourceUrl(url);
};
})
and use it in view :
<iframe ng-src="{{iframe.src | trusted}}"></iframe>