Javascript detect variable as Hyperlink and display it as a Link - javascript

I have a variable in javascript returned by AJAX which may contain a simple string or a href code like EXAMPLE. I have to detect whether it is a link or simple string and display accordingly.
i.e. if it is a link then a hyperLink is to be displayed with text as EXAMPLE or if it is a simple string then it has to be displayed as it is.
I can able to do it in angular using
<span ng-bind-html-unsafe="name_of_variable">
How can I do it in javascript code with javascript variable?

If the variable data contains the response from AJAX, do:
document.getElementById('where_to_put_it').innerHTML = data;
If data looks like a hyperlink, the HTML will be parsed and it will be clickable. If it's plain text, it will just be put into the document that way.
Maybe something like this is what you're looking for with your calendar plugin:
var match = data.match(/<a\s+href=['"](.*?)['"]\s*>(.*?)<\/a>/i);
if (match) {
event = { title: match[2],
url: match[1]
};
} else {
event = { title: data };
}

U need to check the string of data contains url , i believe think below url will help u out.. Check if a Javascript string is a url Regular Expression to find URLs in block of Text (Javascript) How to find if a text contains url string

Related

Hide part of the text returned from the server

On my webpage I want to hide part of the text in an object returned by the server.for example:
<div>
<h4>{{name.SubName}}</h4>
</div>
The string returned by {{name.SubName}} contains a name followed by some text within brackets, like this "Sample Name(XYZ)". I want to be able to hide anything that is appearing within brackets i.e. (XYZ) in this case.
Any suggestions on how I can make this work?
upon returning from server, add a function to the object that returns intended format, like
$.get('example', function(name){
name.cleanSubName = function(){
this.SubName.replace(/\([^)]+\)/, "")
}
});
and use it in the template like,
<h4>{{name.cleanSubName()}}</h4>
regex borrowed from #Rohan Kumar :)
hope this helps.
Better way is to don't respond the name having () from server and if there are () then you can replace it at server side(if the full name is not in use at client side).
In PHP you can use the preg_replace() like,
echo preg_replace("/\([^)]+\)/","","Sample Name(XYZ)"); // 'Sample Name'
And in Javascript you can use,
"Sample Name(XYZ)".replace(/\([^)]+\)/,""); // you need to use name instead of string

How to remove specific querystring on click?

Let's say I have the following url string:
www.mysite.com?this&that&theOtherThing
I know how to add query strings to any link, but am unaware of how to remove one.
For instance, I know I can add a query string by doing the following:
//just add within the href
submit
or
//set the href within jquery
$('button').attr('href', currentUrl + '&that');
How would I remove that same query on click?
I tried the following, but got an NaN error:
$('button').attr('href', currentUrl - '&that');
Does anyone have advice on how to remove a query?
First, you want to replace "?that", not "&that". Second, you have an a tag that doesn't have an id, but you're setting the 'href' attribute of an element with the id of 'button', which doesn't appear in your code snippet.
You got a NaN error because you're trying to subtract "?that" from a string. Instead, use string.replace("?that", "") - which will remove the "%that", but not anything after it (if there is something). If there is something after "&that" in your string, get the indexOf("?that") and then set the string to a substring of itself like so:
html:
Click me
Javascript:
var urlString = $("#linktoModify").prop("href");
urlString = urlString.toString().substring(0, urlString.indexOf("?"));
$("#linkToModify").prop("href", urlString);
Also, keep in mind that your question is a bit misleading. You're trying to edit a string with javascript, not remove a query string. Query strings are readonly and cannot be removed, but you can easily modify a string, which appears to be what you're trying to do.
function remove_args(a, url, arg){
a.attr('href', url.replace(arg,''));
}
html
URL
try something like this. just a small scenario

Javascript: add body ID based on part of search results url

I know just enough JS to get in trouble so please bear with me :)
I am using the WP-Properties WordPress plugin. When searching for properties it gives all results in a common search results page. I need to theme the search results page based on part of the search string so I need a body id.
Example of a search result url:
http://website.com/property/?wpp_search[pagination]=off&wpp_search[property_type]=oasis_park&wpp_search[lot_location]=Oceano+Lot&wpp_search[availability]=-1&wpp_search[phase]=-1&wpp_search[price][min]=-1
The part I want is what comes after: "wpp_search[property_type]"
In the above case it would be "oasis_park"
And this would then create a body tag of: <body id="oasis_park">
I tried to tweak the following code to get the specific part then have it write to the body tag but I can't get it to work in my situation: remove a part of a URL argument string in php
This will only work for this specific url, as you have not provided a general pattern for each url from which you will need to extract a substring:
var myString = "http://website.com/property/?wpp_search[pagination]=off&wpp_search[property_type]=oasis_park&wpp_search[lot_location]=Oceano+Lot&wpp_search[availability]=-1&wpp_search[phase]=-1&wpp_search[price][min]=-1";
var myVar = myString.slice(myString.indexOf("&") + 27, myString.indexOf("k"));
After you have identified a general pattern in every url you wish to check, you will then have to use a combination of substr(), slice() and indexOf() to get the substring you want.
Then, try
document.body.id = myVar;
or assign an id to body (e.g. "myID") then try this:
document.getElementById('myID').id = myVar;

Show new line instead of \n

I am experimenting with HTML5's new LocalStorage and so far I have been successfully in capturing the content (text) inputted onto my form and storing it in the LocalStorage.
My problem starts when a person comes back to the page, I want the LocalStorage values loaded into the correct input on my form.
I am also doing this with success, however my users would see the following:
But I would like them to see the text without the double quotes and new lines characters, so I would like them to see:
Hi
How are you
Just like what they inputted originally. How can I achieve this?
To set the value of the text area, I am using the following:
$(document).ready(function() {
allTextAreasOnPage.each(function(index, entry) {
var idOfTextArea = $(this).attr('id');
if (pageLocalStorage.get(idOfTextArea)) {
$(this).val(pageLocalStorage.get(idOfTextArea));
}
});
});
Looks to me like your string is being stored as JSON...
$(this).val(JSON.parse(pageLocalStorage.get(idOfTextArea)));
Should solve it, but you should look into the cause...
When you link code, make sure you're actually using the code you say you're using.
..

How to edit what is inside a textarea using code in the url?

I'm not sure what the terminology is - but what I would like to do is this:
Using PHP, I would create a dynamic link for users to click that would indicate where they clicked it from. (I know how to do this)
I just don't know what the URL needs to look like to change the contents of a textarea on the target page.
So something like: http://website.com?document.getElementByName'your-message'.innerHTML='test'
Except clearly this doesn't work. Should I instead just put a variable in the URL (I don't know how to do that either) and have the javacript on the actual target page change the textarea content?
Basically I just need it to put one line of text in it. "I came from page x" I'm also willing to change the textarea to an input field if that makes things easier.
That's called a Query String website.com?variable1=value1&variable2=value2&...
Here's an example with just plain ole Javascript: http://www.bloggingdeveloper.com/post/JavaScript-QueryString-ParseGet-QueryString-with-Client-Side-JavaScript.aspx
Also see: How can I get query string values in JavaScript?
You can format your url like this:
www.example.com/?name=john%20blah&age=27&something=meh
then you can parse out the parameters with javascript
var parameterArray = location.search.slice(1).split("&");
var parameterObject = {};
for(i in parameters) {
parameterObject[parameters[i].split("=")[0]] = parameters[i].split("=")[1]
}
then you can populate the fields with the data
nameTxtBox.value = parameterObject.name;

Categories