javascript passing variables via url - javascript

Well, question maybe doesn't correct, but I give a shoot.
My script:
OMG, testing
In real, without javascript url look like:
http://localhost/url=google.com
In here wanna add this: &format=txt
So, the correct url then would be:
http://localhost/url=google.com&format=txt
How to add &format to the javascript "script" which showed at the
top?

is this what you want:
OMG, testing

Just add it to the link using +, unless I misunderstood the question.
OMG, testing
Assumes txt is a variable, otherwise put it all in the quotes.

This will do it ,, you have to do same as you did for the localhost
OMG, testing

like that :
OMG, testing
You only have to add the text to the string of your url

Related

Find and replace regex JavaScript

I know this is a super easy question, but I can't seem to wrap my head about it. I've got a bunch of URLs in varying languages such as:
www.myurl.com?lang=spa
www.myurl.com?lang=deu
www.myurl.com?lang=por
I need to create buttons to quickly switch from any language extension (spa, por, deu, rus, ukr, etc) to another language. I have the following code so far:
var url = window.location.toString();
window.location = url.replace(/lang=xxx/, 'lang=deu');
I just can't figure out the 3-character wildcard character. I know that I need to do some sort of regular expression or something, I'm just not sure how to go about it. Any help?
Thanks in advance
You can use
([&?]lang=)\w+
This will work with urls like www.myurl.com?foo=bar&lang=por&bar=foo too.
Instead of lang=deu, you'll have to replace with $1deu.
Try ... or .{3} or \w{3} or even [a-z]{3}, depending on how specific you want to be.
var s = 'www.myurl.com?lang=spa';
s.replace(/lang=[a-z]{3}/, 'lang=deu');
// => "www.myurl.com?lang=deu"
Here's a railroad diagram of the above example:
Use /lang=[a-z][3}/, here's an example:
/lang=[a-z]{3}/
Debuggex Demo

Pass variable to a javascript function in a c# code

I need to pass a variable to a javascript function,but I got a little trouble.
In the .cs file,I write like this:
string id = "someid";
this.Controls.Add(new LiteralControl("<input type=\"button\" onClick=\"myFunction("+id+")\">"));
I need to use the value of this id,but in the console.log(id),it just shows "object",not the "someid",what's the problem?
Look at the generated HTML:
<input type="button" onClick="myFunction(someid)">
You are generating a variable name when you want a string literal.
Add some quote marks.
Whenever you have a problem that manifests in the browser: Look at the code the browser is dealing with first. You should always start by determining if the JS you want is not working or if the server side code is not generating the JS you want.
just add '' around id as i did below will resolve your issue
this.Controls.Add(new LiteralControl("<input type=\"button\" onClick=\"myFunction('"+id+"')\">"));
Use the single quotes around id:
'"+id+"' (notice the single quotes then double quotes)
The problem is that it is looking for a saved variable on the page named someid, which is why you are getting the object in the console. Pranay Rana has a good solution which is to make sure the string someid is surrounded by single quotes.

Help with replacing a text with regex

I have the following text in a file called build.xml:
component.rollup=true
component.rollup.modules.buildfiles=io-base.xml, io-form.xml, io-xdr.xml
component.rollup_dir=/base
I want it to add one file in it so it will become:
component.rollup=true
component.rollup.modules.buildfiles=io-base.xml, io-form.xml, io-xdr.xml, io-extended.xml
component.rollup_dir=/base
How can I do that with regex?
I'm using javascript.
Thanks!
EDIT:
Forgot to tell you that the files are not static, they may change. Only
component.rollup.modules.buildfiles=
will always be there.
Assuming you have already read the contents of the file:
fileContents.replace(
/^(component\.rollup\.modules\.buildfiles\s*=.*)$/m,
'$1, io-extended.xml');
Try this (assuming the variable with the XML contents in it is called myXmlContent):
if (!myXmlContent.match(/^component\.rollup\.modules\.buildfiles=.*io-extended\.xml/im) {
myXmlContent = myXmlContent.replace(/^(component\.rollup\.modules\.buildfiles=.*)$/im, '$1, io-extended.xml');
This will ensure it isn't added twice.

Getting part of referring url with jquery

I'm trying to set a cookie with a value that is made up of part of a referring url. The referring url looks something like:
http://webct.university.ac.uk/webct/urw/lc715920578061.tp721521425061/courseMenu.dowebct?
and I need to extract:
lc715920578061.tp721521425061
for reuse in another function.
Would this be regex? Could anyone help with the syntax?
Many thanks
Matthew
You can use replace with a regex and split like this:
var desired_part = document.referrer.replace(/^https?:\/\//gi, '').split('/')[3];

How to use this with JQuery

how do i use this code with jquery,
i know it's easy but it doesn't work for me.
document.getElementsByTagName('html')[0].innerHTML.replace(/<!--|-->/g,'')
$('html')[0].innerHTML.replace(/<!--|-->/g,'');
If your intention is to generate a new string where all <!-- and --> are removed then your code works just fine. If not then you probably should be reminded that in javascript, a String's replace() method does not replace anything in that string, it generates a new string. So:
var html = document.getElementsByTagName('html')[0];
html.innerHTML = html.innerHTML.replace(/<!--|-->/g,'');
But your question is very odd. Why would you want to do this? Perhaps you want to uncomment some commented out code? This does not look like something that is safe to do.
This should work for you. Remember you can use javascript and jQuery together. Try this:
$("<html>").html($("<html>").html().replace(/<!--|-->/g,''));
Probably not the most elegant solution, but should work. Are you familiar with:
http://visualjquery.com/

Categories

Resources