Rectangle 27 281

I know this is an old question, but it took me some time to sort this out given the sparse Angular documentation. The RouteProvider and routeParams is the way to go. The route wires up the URL to your Controller/View and the routeParams can be passed into the controller.

Check out the Angular seed project. Within the app.js you'll find an example for the route provider. To use params simply append them like this:

$routeProvider.when('/view1/:param1/:param2', {
    templateUrl: 'partials/partial1.html',    
    controller: 'MyCtrl1'
});

Then in your controller inject $routeParams:

.controller('MyCtrl1', ['$scope','$routeParams', function($scope, $routeParams) {
  var param1 = $routeParams.param1;
  var param2 = $routeParams.param2;
  ...
}]);

Note that the last line of this example }); should be }]);

Also can get other arbitrary params in the query string form /view/1/2?other=12 with $routeParams.other

I think you have the 'var param1' repeated in your controller. I could not edit such a simple change.

Assign and send example: var param1 = "abc"; $location.path('/view1/:' + param1);$route.reload();

javascript - How to get the url parameters using AngularJS - Stack Ove...

javascript angularjs routeparams
Rectangle 27 11

Shouldn't be too difficult to write your own without the need for an external library.

// www.mysite.com/my_app.html?Use_Id=abc 

var GET = {};
var query = window.location.search.substring(1).split("&");
for (var i = 0, max = query.length; i < max; i++)
{
    if (query[i] === "") // check for trailing & with no param
        continue;

    var param = query[i].split("=");
    GET[decodeURIComponent(param[0])] = decodeURIComponent(param[1] || "");
}

Usage: GET.Use_id or GET["Use_id"]. You can also check if a parameter is present even if it has a null value using "Use_id" in GET (will return true or false).

Can you explain the last line? GET[decodeURIComponent(param[0])] = decodeURIComponent(param[1] || "");

How to pass a parameter to a javascript through a url and display it o...

javascript parameters
Rectangle 27 1

$location
$scope.RegisterView
$location.path('/register/5426ced88b49d2e402402205')

This makes sense, because $http.get as mentioned in the question is making a GET request and which doesn't redirect to a URL.

javascript - How to pass parameter(id) in URL using ng-click to view s...

javascript angularjs
Rectangle 27 5

For those looking for a modern solution that works in IE, Firefox, AND Chrome:

None of these solutions that use a hyperlink element will work the same in chrome. If you pass an invalid (or blank) url to chrome, it will always return the host where the script is called from. So in IE you will get blank, whereas in Chrome you will get localhost (or whatever).

If you are trying to look at the referrer, this is deceitful. You will want to make sure that the host you get back was in the original url to deal with this:

function getHostNameFromUrl(url) {
        // <summary>Parses the domain/host from a given url.</summary>
        var a = document.createElement("a");
        a.href = url;

        // Handle chrome which will default to domain where script is called from if invalid
        return url.indexOf(a.hostname) != -1 ? a.hostname : '';
    }

This is a very important thing to consider!

How do I parse a URL into hostname and path in javascript? - Stack Ove...

javascript url
Rectangle 27 4

www.mysite.com/my_app.html?Use_Id=abc

Then in that page use a javascript function like:

var urlParam = function(name, w){
    w = w || window;
    var rx = new RegExp('[\&|\?]'+name+'=([^\&\#]+)'),
        val = w.location.search.match(rx);
    return !val ? '':val[1];
}
var useId = urlParam('Use_Id');

The second parameter w is optional, but useful if you want to read parameters on iframes or parent windows.

Your code is pretty useful! Thank, you. What about post method like in php?

@S.P.H.I.N.X when you make a post to a PHP page, there is no client javascript anymore. You need to do a parsing with PHP on the server either data posted or in the url like here.

How to pass a parameter to a javascript through a url and display it o...

javascript parameters
Rectangle 27 2

In your example parts of your passed-in URL are not URL encoded (for example the colon should be %3A, the forward slashes should be %2F). It looks like you have encoded the parameters to your parameter URL, but not the parameter URL itself. Try encoding it as well. You can use encodeURIComponent.

javascript - How do I pass a URL with multiple parameters into a URL? ...

javascript html query-parameters
Rectangle 27 5

You can try encodeUri Built-in function, for example

encodeURI('coo funcion!')

Where I have to put this? I want to have the html in an android and ios app. So I would have to encode parameters before adding them, don't I?

Is easier to get all the url with your parameters and then just encode it before send it.

html - Encode special characters to pass in url and read by javascript...

javascript html
Rectangle 27 5

You can try encodeUri Built-in function, for example

encodeURI('coo funcion!')

Where I have to put this? I want to have the html in an android and ios app. So I would have to encode parameters before adding them, don't I?

Is easier to get all the url with your parameters and then just encode it before send it.

html - Encode special characters to pass in url and read by javascript...

javascript html
Rectangle 27 6

ajax: {
  // The number of milliseconds to wait for the user to stop typing before
  // issuing the ajax request.
  delay: 250,
  // You can craft a custom url based on the parameters that are passed into the
  // request. This is useful if you are using a framework which has
  // JavaScript-based functions for generating the urls to make requests to.
  //
  // @param params The object containing the parameters used to generate the
  //   request.
  // @returns The url that the request should be made to.
  url: function(params) {
    return UrlGenerator.Random();
  },
  // You can pass custom data into the request based on the parameters used to
  // make the request. For `GET` requests, the default method, these are the
  // query parameters that are appended to the url. For `POST` requests, this
  // is the form data that will be passed into the request. For other requests,
  // the data returned from here should be customized based on what jQuery and
  // your server are expecting.
  //
  // @param params The object containing the parameters used to generate the
  //   request.
  // @returns Data to be directly passed into the request.
  data: function(params) {
    var queryParameters = {
      q: params.term
    }

    return queryParameters;
  },
  // You can modify the results that are returned from the server, allowing you
  // to make last-minute changes to the data, or find the correct part of the
  // response to pass to Select2. Keep in mind that results should be passed as
  // an array of objects.
  //
  // @param data The data as it is returned directly by jQuery.
  // @returns An object containing the results data as well as any required
  //   metadata that is used by plugins. The object should contain an array of
  //   data objects as the `results` key.
  processResults: function(data) {
    return {
      results: data
    };
  },
  // You can use a custom AJAX transport function if you do not want to use the
  // default one provided by jQuery.
  //
  // @param params The object containing the parameters used to generate the
  //   request.
  // @param success A callback function that takes `data`, the results from the
  //   request.
  // @param failure A callback function that indicates that the request could
  //   not be completed.
  // @returns An object that has an `abort` function that can be called to abort
  //   the request if needed.
  transport: function(params, success, failure) {
    var $request = $.ajax(params);

    $request.then(success);
    $request.fail(failure);

    return $request;
  }
}

on the processResult function use :

processResults: function(data) {
   $('select#test').select2("val", YOUR VALUE FROM PROCESSED DATA); //set the value
   return {
     results: data
   };
 }

javascript - Select2 does not know the corresponding text of selected ...

javascript angularjs jquery-select2
Rectangle 27 1

You don't have to use an Action Link. Obviously, you do not know the URL that you wish to send when rendering server-side.

Render only the content that you know can be rendered at that time (i.e the route + the item ID)(custom extension?) and adjust the rest via a JS function.

button.onClick = () => {
   window.open(getURL(), "_blank");
}

javascript - How to pass current Url with query strings into Html.Acti...

javascript asp.net-mvc asp.net-mvc-4 razor html.actionlink
Rectangle 27 66

html encoding
URL encode
http://www.facebook.com/sharer.php?&t=FOOBAR&u=http%3A%2F%2Fwww.foobar.com%2F%3Ffirst%3D12%26sec%3D25%26position%3D

You can do this easily in most languages - in javascript:

var encodedParam = encodeURIComponent('www.foobar.com/?first=1&second=12&third=5');
// encodedParam = 'http%3A%2F%2Fwww.foobar.com%2F%3Ffirst%3D12%26sec%3D25%26position%3D'

(there are equivalent methods in other languages too)

javascript - How do I pass a URL with multiple parameters into a URL? ...

javascript html query-parameters
Rectangle 27 1

I believe that the only way to pass a javascript object from one page to another is to serialize it into string and pass it in url. For example if you have object

var temp = { id: 1, value: 'test' }

you may want to use JSON-js to serialize it and pass it in for example http://mysite.com/?data=serialization. Then after you load the page you need to deserialize it via for example $.parseJSON().

jquery - How to pass javascript object from one page to other - Stack ...

javascript jquery
Rectangle 27 2

... need to pass parameters to state, but not via url...

we can solve it by state setting: params : {}.

'guy2'
.state('guy2', {
    parent: 'guys',
     url: "/:urlParam",
     params: { 
       urlParam: null,
       param1: null,
       param2: null,
       param3: null,
    },
    controller:'MyCtrl',
    templateUrl: "guy.html",
})

The urlParam will be part of url, while the others won't. We can call it like this:

<a ui-sref="guy2({
      urlParam: guy.name, 
      param1: guy.desc,
      param2: guy.note,
      param3: guy.info,
})">{{guy.name}}</a>

and for data.json like this:

[
    {

    "name":"Jane",
    "desc":"Jane descrition ...",
    "info":"Jane Info ...",
    "note":"Jane Note ..."
    },
    ...
]
#/route/Jane

Great if that helped ;) Really. Enjoy mighty UI-Router... which I just proudly consume ... as you ;)

javascript - A better way to inject data in controller using statepara...

javascript json angularjs angular-ui-router state
Rectangle 27 1

You should be able to pass params to the calc() function using the following syntax:

ScriptManager.RegisterClientScriptBlock(Page,typeof(Page),"func","Calc('" & param1 & "','" & param2 & "');",true)

Thanks for your quick reply.But my problem is lil bit different.I dont want to pass parameters in my javascript function.I want to call my function from .cs page when the page url contains Query String

javascript - How to Pass Query String with Page Url in RegisterClientS...

javascript c# asp.net asp.net-ajax
Rectangle 27 16

Yes it does: Question "Can my JS directly read the value of this var like this?" Answer "You can't passe variables to JS the way you tried"

Although, the src attr can point to a php (or any other server side tech) and accomplish this.

html - Pass vars to JavaScript via the SRC attribute - Stack Overflow

javascript html url variables src
Rectangle 27 11

Within your onDropDownChange handler, just make a jQuery AJAX call, passing in any data you need to pass up to your URL. You can handle successful and failure calls with the success and error options. In the success option, use the data contained in the data argument to do whatever rendering you need to do. Remember these are asynchronous by default!

function onDropDownChange(e) {
    var url = '/Home/Index/' + e.value;
    $.ajax({
      url: url,
      data: {}, //parameters go here in object literal form
      type: 'GET',
      datatype: 'json',
      success: function(data) { alert('got here with data'); },
      error: function() { alert('something bad happened'); }
    });
}

jquery - How to call URL action in MVC with javascript function? - Sta...

jquery asp.net-mvc razor
Rectangle 27 5

From what i could read from the select2 documentation, you should pass string or function to ajax url parameter. So this is what i've done I've written a function that returns value of the observable (which is the url):

self.returnUrl = function () {
    return self.dynamicUrl();
};
<input data-bind="combobox: { ... sourceUrl: $data.returnUrl }, value: ApplyToSubject" type="hidden" >

Then the custom binding works the same as in the code in the question, with a small change:

...
ajax: {
     url: sourceUrl <- this is the returnUrl function
...
}

javascript - select2 change ajax url - Stack Overflow

javascript jquery knockout.js jquery-select2
Rectangle 27 2

You can try to investigate these functions in w3schools.com. Here are the links with basic information and live "Try it out" feature:

  • encodeURI - takes string with your characters and encodes it into plausible for url style ( encoding spaces and non ANSII chars )

html - Encode special characters to pass in url and read by javascript...

javascript html
Rectangle 27 2

You can try to investigate these functions in w3schools.com. Here are the links with basic information and live "Try it out" feature:

  • encodeURI - takes string with your characters and encodes it into plausible for url style ( encoding spaces and non ANSII chars )

html - Encode special characters to pass in url and read by javascript...

javascript html
Rectangle 27 204

The second question is actually easier to answer. Look at the stringWithContentsOfURL:encoding:error: method of NSString - it lets you pass in a URL as an instance of NSURL (which can easily be instantiated from NSString) and returns a string with the complete contents of the page at that URL. For example:

NSString *googleString = @"http://www.google.com";
NSURL *googleURL = [NSURL URLWithString:googleString];
NSError *error;
NSString *googlePage = [NSString stringWithContentsOfURL:googleURL 
                                                encoding:NSASCIIStringEncoding
                                                   error:&error];

After running this code, googlePage will contain the HTML for www.google.com, and error will contain any errors encountered in the fetch. (You should check the contents of error after the fetch.)

Going the other way (from a UIWebView) is a bit trickier, but is basically the same concept. You'll have to pull the request from the view, then do the fetch as before:

NSURL *requestURL = [[yourWebView request] URL];
NSError *error;
NSString *page = [NSString stringWithContentsOfURL:requestURL 
                                          encoding:NSASCIIStringEncoding
                                             error:&error];

EDIT: Both these methods take a performance hit, however, since they do the request twice. You can get around this by grabbing the content from a currently-loaded UIWebView using its stringByEvaluatingJavascriptFromString: method, as such:

NSString *html = [yourWebView stringByEvaluatingJavaScriptFromString: 
                                         @"document.body.innerHTML"];

This will grab the current HTML contents of the view using the Document Object Model, parse the Javascript, then give it to you as an NSString* of HTML.

Another way is to do your request programmatically first, then load the UIWebView from what you requested. Let's say you take the second example above, where you have NSString *page as the result of a call to stringWithContentsOfURL:encoding:error:. You can then push that string into the web view using loadHTMLString:baseURL:, assuming you also held on to the NSURL you requested:

[yourWebView loadHTMLString:page baseURL:requestURL];

I'm not sure, however, if this will run Javascript found in the page you load (the method name, loadHTMLString, is somewhat ambiguous, and the docs don't say much about it).

Awesome! Thanks for the great answer. I presume both methods result in the page being loaded twice, which may have a performance impact. Is there a way to avoid that?

As a matter of fact, there are :) Edited answer.

Yes, [yourWebView loadHTMLString:page baseURL:requestURL]; will run the Javascript in the page. I've used this api with Google maps.

NSString *html = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"];

iphone - Reading HTML content from a UIWebView - Stack Overflow

html iphone uiwebview